簡體   English   中英

無法將關鍵字“文章”解析為字段

[英]Cannot resolve keyword 'article' into field

我知道已經有多個問題,但是沒有一個可以解決我的問題。

我將模型分成多個文件夾,分別位於models文件夾下,即models / articles.py,models / core.py等。

當我嘗試執行

User.objects.annotate(my_count=Count('article'))

django給我以下錯誤:

FieldError:無法將關鍵字“文章”解析為字段。 選項包括:訪問權限,cfi_store_item_likes,協作者,注釋,date_joined,文檔,電子郵件,電子郵件地址,first_name,組,id,圖像,is_active,is_staff,is_superuser,last_login,last_name,列表,logentry,makey,makey_removed,newproduct,便箋,密碼,產品說明,產品圖片,個人資料,社交帳戶,space_admins,space_members,textdocumentation,教程,user_permissions,userflags,用戶名,視頻

我在models / abstract.py中有以下代碼

class BaseModel(models.Model):
    added_time = models.DateTimeField('added time')
    is_enabled = models.BooleanField(default=True)
    score = models.IntegerField(default=0)

    class Meta:
        abstract = True
        app_label = 'catalog'

我在models / article.py中有以下內容

class Article(BaseModel):
    url = models.URLField()
    title = models.CharField(max_length=500, null=True, blank=True)
    description = models.TextField(null=True, blank=True)
    image_url = models.URLField(null=True, blank=True)
    rating = models.IntegerField()
    recommendation = models.TextField(null=True, blank=True)
    user = models.ForeignKey(User, null=True, blank=True)
    tags = models.ManyToManyField(ArticleTag, null=True, blank=True)
    comments = models.ManyToManyField(Comment, null=True, blank=True)
    new_user = models.ForeignKey('NewUser', null=True, blank=True)

    class Meta:
        app_label = 'catalog'

    def __unicode__(self):
        return self.title + ' (' + self.url + ')'

我在models / core.py中有以下內容,以及已列為可用選項的許多其他模型。

class Tutorial(BaseModel):
    user = models.ForeignKey(User, null=True, blank=True)
    description = models.CharField(max_length=200, null=True, blank=True)
    url = models.URLField(max_length=200)
    votes = models.IntegerField(default=0)
    images = models.ManyToManyField(Image, related_name="tutorialimages",
                                    null=True, blank=True)

    class Meta:
        app_label = 'catalog'

    def __unicode__(self):
        return self.url

為什么django不從其余文件的模型中提取我的ForeignKey給用戶? 為什么只從core.py獲取它?

我已經在http://pastebin.com/v6hFdvAChttp://pastebin.com/nxYktwHn上發布了模型和stacktrace。

因為並非所有用戶都有一篇文章。 如果看到,您在Article Class用戶外鍵可以為null。

user = models.ForeignKey(User, null=True, blank=True)

然后,當Django嘗試將計數應用於空值時,就會引發錯誤。

也許您可以使用這樣的東西:

User.objects.objects.filter(article__isnull=False).count()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM