Django 系统检查发现3个问题(0个静音)
我的model.py可能会出现什么问题。我尝试了所有方法,但什么都没有发生。我认为我定义外键的方法是正确的。这可能是我定义的问题,还是我必须在我的外键中使用Django 系统检查发现3个问题(0个静音),django,sqlite,django-models,fedora-25,Django,Sqlite,Django Models,Fedora 25,我的model.py可能会出现什么问题。我尝试了所有方法,但什么都没有发生。我认为我定义外键的方法是正确的。这可能是我定义的问题,还是我必须在我的外键中使用memberid.user,或者会产生什么效果。欢迎任何贡献 Performing system checks... Unhandled exception in thread started by <function wrapper at 0x7f6a926d69b0> Traceback (most recent call
memberid.user
,或者会产生什么效果。欢迎任何贡献
Performing system checks...
Unhandled exception in thread started by <function wrapper at 0x7f6a926d69b0>
Traceback (most recent call last):
File "/usr/lib64/python2.7/site-packages/django/utils/autoreload.py", line 227, in wrapper
fn(*args, **kwargs)
File "/usr/lib64/python2.7/site-packages/django/core/management/commands/runserver.py", line 125, in inner_run
self.check(display_num_errors=True)
File "/usr/lib64/python2.7/site-packages/django/core/management/base.py", line 405, in check
raise SystemCheckError(msg)
django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:
ERRORS:
tithe.tithe.memberid: (fields.E300) Field defines a relation with model 'memberid', which is either not installed, or is abstract.
tithe.tithe.memberid: (fields.E307) The field tithe.tithe.memberid was declared with a lazy reference to 'tithe.memberid', but app 'tithe' doesn't provide model 'memberid'.
tithe.tithe: (models.E012) 'unique_together' refers to the non-existent field 'IntegerField'.
System check identified 3 issues (0 silenced).
Performing system checks...
Unhandled exception in thread started by <function wrapper at 0x7f3d3ccdc9b0>
Traceback (most recent call last):
File "/usr/lib64/python2.7/site-packages/django/utils/autoreload.py", line 227, in wrapper
fn(*args, **kwargs)
File "/usr/lib64/python2.7/site-packages/django/core/management/commands/runserver.py", line 125, in inner_run
self.check(display_num_errors=True)
File "/usr/lib64/python2.7/site-packages/django/core/management/base.py", line 405, in check
raise SystemCheckError(msg)
django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:
ERRORS:
tithe.tithe.memberid: (fields.E300) Field defines a relation with model 'User', which is either not installed, or is abstract.
tithe.tithe.memberid: (fields.E307) The field tithe.tithe.memberid was declared with a lazy reference to 'tithe.user', but app 'tithe' doesn't provide model 'user'.
tithe.tithe: (models.E012) 'unique_together' refers to the non-existent field 'IntegerField'.
下一行
memberid = models.ForeignKey('User')
这是问题的根源。您必须向它传递一个用户对象
导入用户
模型
from django.contrib.auth.models import User
然后
您的ForeignKey需要引用具体模型或抽象模型。由于您使用字符串进行引用(这意味着模型是抽象的),因此需要通过声明abstract=True将Meta:as类声明为抽象的 当模型子类化为具体模型且与抽象模型的app_标签无关时,以这种方式在抽象模型上定义的关系将得到解决: 以下信息来自Django文档
前两个警告是因为Django找不到您在
memberid
外键中引用的型号'User'
我建议您使用settings.AUTH\u USER\u MODEL
来引用用户模型。无论您是否有自定义用户模型,这都将起作用
memberid = models.ForeignKey(settings.AUTH_USER_MODEL)
有关详细信息,请参阅文档
请注意,最好将您的字段命名为成员
。这样,相关实例将是member
,相关id将是member\u id
。目前,相关实例是memberid
,相关id是memberid\u id
最后一个警告是因为模型中没有字段IntegerField
。如果希望receitcode
字段本身是唯一的,则将unique\u一起删除
行,并将字段更改为:
receitcode = models.CharField(max_length=45, unique=True)
我可以更改为
memberid-models.ForeignKey('memberid.User')
memberid.User将无法工作。memberid没有任何名为User的属性。默认用户模型将在这里工作。此外,您不必将其作为字符串传递。因为我希望收据具有唯一代码,所以我已将其选择为unique\u-together=[“receipt\u-code”]
我已将memberid
更改为memberid
并将membername
更改为member
我认为这是有意义的,现在您不需要一起使用unique\u来使单个字段唯一。只需在字段中使用unique
选项。您误解了我关于外键的观点。外键不应包含id
。请参阅文档中关于[数据库表示法](docs.djangoproject.com/en/1.11/ref/models/fields/#database representation)的注释,这就是我无法迁移的原因。在应用程序“tithe”中未检测到任何更改。
products/models.py
from django.db import models
class AbstractCar(models.Model):
manufacturer = models.ForeignKey('Manufacturer', on_delete=models.CASCADE)
# This is what you need to add
class Meta:
abstract = True
memberid = models.ForeignKey(settings.AUTH_USER_MODEL)
receitcode = models.CharField(max_length=45, unique=True)