为什么 Post 没有“发布”字段,因为应用程序缓存尚未准备好,所以如果这是自动创建的字段,它还没有准备好?

问题描述 投票:0回答:1

我是 Django 的初学者。我正在做一个博客网站项目。我正在为博客创建一个数据库。但是当我运行命令

python manage.py makemigrations blog
时,我收到此错误。

错误:

FieldDoesNotExist(django.core.exceptions.FieldDoesNotExist: 帖子有 没有名为“发布”的字段。应用程序缓存尚未准备好,因此如果是这样 自动创建的相关字段,目前尚不可用。)

这是我的blog.models.py文件:

from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User

# Create your models here.
class Post(models.Model):
    
    class Status(models.TextChoices):
        DRAFT = 'DF', 'Draft'
        PUBLISHED = 'PB', 'Published'
    
    title = models.CharField(max_length=250)
    slug = models.SlugField(max_length=250)
    author = models.ForeignKey(User,on_delete=models.CASCADE,related_name='blog_posts')
    body = models.TextField()
    published = models.DateTimeField(default=timezone.now)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    status = models.CharField(max_length=2,choices=Status.choices,default=Status.DRAFT)

    class Meta:
        ordering = ['-publish']
        indexes = [ models.Index(fields=['-publish']),]

    def __str__(self):
        return self. Title

我不知道问题所在。有人可以帮助我吗?

python django
1个回答
0
投票

您在索引和排序定义中引用了不存在的字段

publish

class Meta:
    ordering = ['-publish']
#                 ^^^^^^^
    indexes = [ models.Index(fields=['-publish']),]
#                                      ^^^^^^^

您可能想引用

published
字段

© www.soinside.com 2019 - 2024. All rights reserved.