2017-08-11 53 views
1

什麼可能與我的model.py .i的問題已經嘗試了一切都沒有發生。我想我定義我的外鍵正確的方式。它可能是一個問題與我的定義或我是否需要在我的foreginkey中使用memberid.user或有什麼作用。歡迎任何貢獻。系統檢查發現3個問題(0沉默)

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"] 

回答

1

前兩個警告是因爲Django中找不到你指的是在模型'User'memberid外鍵。

我建議您使用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) 
+0

因爲我想收據有一個唯一的代碼,我選擇它是'unique_together = [「receipt_code」]' – harri

+0

我已經改變'memberid'到'member_id'和'membername'是'member'i認爲這使得現在感覺 – harri

+0

你不需要'unique_together'來使一個字段唯一。只需使用該字段上的「唯一」選項即可。 – Alasdair

1

下面

memberid = models.ForeignKey('User') 

該生產線是造成問題的原因。你必須傳遞一個User對象給它。

導入User模型。

from django.contrib.auth.models import User 

然後

memberid = models.ForeignKey(User) 
+0

我可以改變'MEMBERID - models.ForeignKey(「memberid.User」)' – harri

+0

MEMBERID。用戶將無法工作。 memberid沒有任何名爲User的屬性。默認的用戶模型將在這裏工作。你也不必將它作爲字符串傳遞。 –

0

你ForeignKey的需要引用一個具體的模型或抽象模型。由於您使用的是字符串(這意味着模型是抽象的),所以您需要聲明類Meta:摘要如下:abstract as = abstract

這種在抽象模型上定義的關係在模型被子類化爲具體型號和相對抽象模型的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