我的模型可能有什么问题。py。我已经尝试了一切,但没有发生任何事情。我认为我定义外键的方式是正确的。这是我定义的问题吗?还是我必须在我的foreginkey中使用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'.
这是我的型号.py代码
from django.utils import timezone
from django.db import models
# Create your models here.
class tithe(models.Model):
memberid = models.ForeignKey('User')
membername = models.CharField(max_length=45)
receitcode = models.CharField(max_length=45)
tithes = models.IntegerField()
combinedoffering = models.IntegerField()
campmeetingoffering = models.IntegerField()
churchbuilding = models.IntegerField()
conference = models.IntegerField()
localchurch = models.IntegerField()
funds = models.IntegerField()
total = models.IntegerField()
created_date = models.DateTimeField(
default=timezone.now)
published_date = models.DateTimeField(
blank=True, null=True)
def publish(self):
self.published_date = timezone.now()
self.save()
def __str__(self):
return self.receitcode
class Meta:
unique_together = ["receitcode","IntegerField"]
ordering = ["published_date","membername"]
下方的行
memberid = models.ForeignKey('User')
是造成问题的原因。你必须将一个User对象传递给它。
导入User
模型。
from django.contrib.auth.models import User
然后
memberid = models.ForeignKey(User)
memberid
外键中引用的模型'User'
。
我建议您使用settings.AUTH_USER_MODEL
来参考用户模型。无论您是否有自定义用户模型,这都会起作用。
memberid = models.ForeignKey(settings.AUTH_USER_MODEL)
有关引用用户模型的更多信息,请参阅文档。
请注意,最好将字段命名为member
。这样,相关的实例将是member
,而相关的id将是member_id
。目前,相关实例为memberid
,相关id为memberid_id
。
最后一个警告是因为模型中没有字段IntegerField
。如果您希望receitcode
字段本身是唯一的,则删除unique_together
行并将字段更改为:
receitcode = models.CharField(max_length=45, unique=True)
您的ForeignKey需要引用具体模型或抽象模型。由于您使用字符串进行引用(这意味着模型是抽象的),因此需要通过声明abstract=True来将类Meta:声明为抽象
当模型被子类化为具体模型并且与抽象模型的app_label:无关时,在抽象模型上以这种方式定义的关系就会得到解决
以下信息来自Django文档https://docs.djangoproject.com/en/1.11/ref/models/fields/
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