gpt4 book ai didi

python - 使用 Django 从其他应用程序导入模型时遇到问题

转载 作者:太空宇宙 更新时间:2023-11-03 13:22:02 25 4
gpt4 key购买 nike

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

“行政”模式:

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 前面,如果我只是尝试导入(没有#)模型契约(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'

到目前为止,我到处都看并尝试了很多选项..但错误仍然存​​在...我无法理解...我阅读了循环导入等...并尝试使用路径进行引用(如您所见), 但效果不佳...

任何建议都将不胜感激...谢谢

最佳答案

您的回溯显示错误与类 ContractForm 有关您在 administrative/models.py 中定义的在第 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):
...

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

from administrative.models import Contract

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

关于python - 使用 Django 从其他应用程序导入模型时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10598317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com