系统检查确定了 3 个问题(0 个已沉默)

System check identified 3 issues (0 silenced)

我的 model.py 有什么问题。我什么都试过了 happens.and 我想我定义外键的方式是正确的。这可能是我定义的问题还是我有在我的外键中使用 memberid.user 或者 effect.any 欢迎贡献。

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

这是我的 model.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模型。

from django.contrib.auth.models import User

然后

memberid = models.ForeignKey(User)

您的外键需要引用具体模型或抽象模型。由于您使用字符串进行引用(这意味着模型是抽象的),因此您需要通过声明 abstract = True

将 class Meta: 声明为抽象

当模型被子class为具体模型并且与抽象模型的 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

前两个警告是因为 Django 找不到您在 memberid 外键中引用的模型 'User'

我推荐你使用settings.AUTH_USER_MODEL来引用用户模型。无论您是否拥有自定义用户模型,这都将起作用。

memberid = models.ForeignKey(settings.AUTH_USER_MODEL)

有关 referencing the user model 的更多信息,请参阅文档。

请注意,最好将您的字段命名为 member。这样,相关实例将是 member,相关 ID 将是 member_id。目前关联实例为memberid,关联id为memberid_id.

最后一个警告是因为您在模型中没有字段 IntegerField。如果您希望 receitcode 字段本身是唯一的,则删除 unique_together 行并将该字段更改为:

receitcode = models.CharField(max_length=45, unique=True)