Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: missing class attribute __module__ when using type()?

Tags:

python

django

I'm attempting to dynamically construct some classes which are subclasses of django's models.Model. My code looks like this:

MyModel = type('MyModel', (models.Model,), {
    'field': models.BooleanField()
    })

When Django tries to load this, it gives the following exception:

Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 88, in inner_run
    self.validate(display_num_errors=True)
  File "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 249, in validate
    num_errors = get_validation_errors(s, app)
  File "/usr/lib/python2.7/site-packages/django/core/management/validation.py", line 36, in get_validation_errors
    for (app_name, error) in get_app_errors().items():
  File "/usr/lib/python2.7/site-packages/django/db/models/loading.py", line 146, in get_app_errors
    self._populate()
  File "/usr/lib/python2.7/site-packages/django/db/models/loading.py", line 61, in _populate
    self.load_app(app_name, True)
  File "/usr/lib/python2.7/site-packages/django/db/models/loading.py", line 78, in load_app
    models = import_module('.models', app_name)
  File "/usr/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/home/user/project/app/models.py", line 221, in <module>
    'field': models.BooleanField()
  File "/usr/lib/python2.7/site-packages/django/db/models/base.py", line 38, in __new__
    module = attrs.pop('__module__')
KeyError: '__module__'

It seems like a class constructed using type() isn't getting the __module__ member which is present for class defined with class. What's going on here?

like image 549
kdt Avatar asked Sep 06 '11 13:09

kdt


1 Answers

May be metaclasses is best choice for you needs than low-level type()?

But in most cases you can use __name__ as a value for __module__ like

MyModel = type('MyModel', (models.Model,), {
    'field': models.BooleanField(),
    '__module__': __name__,
})

Django uses __module__ value for determine which django app owns this model, so models in my_app1/models.py will contain my_app1.models and django knows that models from that file|module belongs to my_app1 application.

like image 54
seriyPS Avatar answered Sep 18 '22 17:09

seriyPS