[Django]-Trouble importing model from other app with Django

7👍

âś…

Your traceback shows the error is with class ContractForm which you have defined in administrative/models.py on line 28

...
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):
...

Since 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

from administrative.models import Contract

Python does not just select that one class from the administrative/models.py file. 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 name Contract from 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 class ContractForm. The rest of the traceback details what exactly went wrong with that class.

1👍

If i am not wrong .If you are importing the Contract model and its seems that those two apps are kept in apps folder so you have to add apps while importing

from apps.administrative.models import Contract

you should use this model directly like bellow

FK_Contract = models.ForeignKey(Contract, unique=False, blank=True, null=True, on_delete=models.SET_NULL)

-1👍

I just hope that the indentation problems in the code you posted in this question are not acctually happpening in your real code.

For example, your administrative/models.py should look like this:

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')

Some tips:

  1. DO NOT name your model fields capitalized. Python’s best practice is date_to, date_from, contract, and so on. Check PEP 8 for details.
  2. It’s also a bad practice to preceed ForeignKey field with “FK_”. Just redundant. Of course this is just my opinion. Maybe you are using some naming standard…. But if you could avoid this, it would be better.
  3. Add the verbose_name param to your field definitions.
  4. Add default values for null=True, blank=True fields.
  5. Be verbose (give meaningful names) to your models. Crefcontr2num is a bad one.

Hope it helps.

Leave a comment