I’m in trouble trying to import a model from another app. I’ve two apps “main” and “administrative”. Here the code, where I stripped out some verbose description :
“administrative” model:
from django.db import models
from django import forms
from django.forms import ModelForm
class Contract(models.Model):
Code = models.CharField(max_length=50)
Provider = models.CharField(max_length=30)
Description = models.CharField(max_length=30)
ActivationDate = models.DateField(blank=True, null=True)
EndingDate = models.DateField(blank=True, null=True)
Note = models.TextField(blank=True)
Numbers = models.ManyToManyField('main.Number', through='Crefcontr2num')
def __unicode__(self):
return u'%s %s' % (self.Provider, self.Description)
class Crefcontr2num(models.Model):
Dateto = models.DateField()
Datefrom = models.DateField()
Contract = models.ForeignKey('Contract')
Num = models.ForeignKey('main.Number')
“main” model:
from django.db import models
from endusers.models import OrderedEndUser
from django import forms
from django.forms import ModelForm, Textarea, TextInput, HiddenInput
#from administrative.models import Contract
class Device(models.Model):
Maker = models.CharField(error_messages={'required': 'need !'})
Model = models.CharField(max_length=30, blank=True)
Imei = models.CharField(max_length=15, unique=True)
Note = models.TextField(blank=True)
ActiveState = models.BooleanField()
AcquisitionDate = models.DateField(blank=True, null=True)
DismissionDate = models.DateField(blank=True, null=True)
CodInv = models.CharField(max_length=15, blank=True)
FK_Enduser = models.ForeignKey('endusers.OrderedEndUser',unique=False, blank=True, null=True, on_delete=models.SET_NULL)
# FK_Contract = models.ForeignKey(administrative.Contract, unique=False, blank=True, null=True, on_delete=models.SET_NULL)
…please note “”#” up in import and in front of FK_Contract in model “device”, if I simply try to import (without #) the model Contract, I’ve got this error :
Unhandled exception in thread started by <bound method Command.inner_run of <django.contrib.staticfiles.management.commands.runserver.Command object at 0xb6f69a6c>>
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py", line 88, in inner_run
self.validate(display_num_errors=True)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 249, in validate
num_errors = get_validation_errors(s, app)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/validation.py", line 35, in get_validation_errors
for (app_name, error) in get_app_errors().items():
File "/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py", line 146, in get_app_errors
self._populate()
File "/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py", line 61, in _populate
self.load_app(app_name, True)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py", line 78, in load_app
models = import_module('.models', app_name)
File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/media/truecrypt1/develope/Django-1.3.1/dbMobile/../dbMobile/main/models.py", line 5, in <module>
from administrative.models import Contract #, Crefcontr2num
File "/media/truecrypt1/develope/Django-1.3.1/dbMobile/administrative/models.py", line 28, in <module>
class ContractForm(ModelForm):
File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py", line 205, in __new__
opts.exclude, opts.widgets, formfield_callback)
File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py", line 159, in fields_for_model
formfield = f.formfield(**kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py", line 1155, in formfield
'queryset': self.rel.to._default_manager.using(db).complex_filter(self.rel.limit_choices_to)
AttributeError: 'str' object has no attribute '_default_manager'
I look so far everywhere and tried many option.. but error is still there… I’m unable to understand… I read about circular import etc… and tried referring with path (as you can see), but it doesn’t work as well…
any advice is appreciated…
thx
Your traceback shows the error is with class
ContractFormwhich you have defined inadministrative/models.pyon line 28Since you haven’t included that part of your code in your question, there’s not much more i can tell you.
However you should be aware how importing in Python works. When you use
Python does not just select that one class from the
administrative/models.pyfile. Instead the Python interpreter reads the entire file, creates the module object, executes the code from the imported file in the new modules namespace, and then copies the nameContractfrom the new module’s namespace to the current namespace. So, although it seems that you are importing just one class from a module, an error anywhere in that module can prevent a successful import – in your case it is an error with classContractForm. The rest of the traceback details what exactly went wrong with that class.