无法使用Django从其他应用程序导入模型

时间:2012-05-15 09:58:49

标签: python django model import

我在尝试从其他应用导入模型时遇到了麻烦。我有两个应用程序“主要”和“管理”。这里是代码,我在其中删除了一些冗长的描述:

“行政”模式:

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

“主要”型号:

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)

...请注意“#”在导入和模型“设备”中的FK_Contract前面,如果我只是尝试导入(没有#)模型合同,我有这样的错误:

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'

到目前为止我看到的地方到目前为止都尝试了很多选项..但是错误仍然存​​在......我无法理解......我读到了关于循环导入等...并试着用路径引用(如你所见) ),但它不起作用......

任何建议表示赞赏...... THX

3 个答案:

答案 0 :(得分:7)

您的追踪显示的错误是您在第28行的ContractForm中定义的课程administrative/models.py

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

由于你没有在你的问题中包含这部分代码,所以我可以告诉你更多。 但是你应该知道Python中的导入是如何工作的。当你使用

from administrative.models import Contract

Python不只是从administrative/models.py文件中选择一个类。相反,Python解释器读取整个文件,创建模块对象,从新模块命名空间中的导入文件执行代码,然后将名称Contract从新模块的命名空间复制到当前命名空间。因此,虽然您似乎只是从模块中导入一个类,但该模块中的任何地方错误都可能阻止导入成功 - 在您的情况下,类ContractForm出错。其余的追溯详细说明了该类究竟出了什么问题。

答案 1 :(得分:1)

如果我没有错。如果您要导入合同模型,似乎这两个应用程序保存在apps文件夹中,因此您必须在导入时添加应用程序

from apps.administrative.models import Contract

你应该像bellow

一样直接使用这个模型
FK_Contract = models.ForeignKey(Contract, unique=False, blank=True, null=True, on_delete=models.SET_NULL)

答案 2 :(得分:-1)

我只是希望你在这个问题中发布的代码中的缩进问题在你的真实代码中并没有真正开心。

例如,您的administrative/models.py应如下所示:

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

一些提示:

  1. 请勿将模型字段命名为大写。 Python的最佳做法是date_todate_fromcontract,依此类推。检查PEP 8 for details
  2. 将ForeignKey字段置于“FK_”之前也是一种不好的做法。只是多余的。当然这只是我的意见。也许你正在使用一些命名标准....但如果你能避免这种情况,那就更好了。
  3. verbose_name参数添加到字段定义中。
  4. default字段添加null=True, blank=True个值。
  5. 为您的模型添加详细信息(提供有意义的名称)。 Crefcontr2num是一个糟糕的。
  6. 希望它有所帮助。