为什么Post没有'publish'字段,应用缓存尚未准备好,如果这是一个自动创建的字段,它还没有准备好?
我刚开始学习Django,正在做一个博客网站项目。我正在为博客创建一个数据库。但是当我运行命令 python manage.py makemigrations blog
时,出现了这个错误。
错误信息:
FieldDoesNotExist(django.core.exceptions.FieldDoesNotExist: Post 没有名为 'publish' 的字段。应用缓存还没有准备好,所以如果这是一个自动创建的相关字段,它还不可用。)
这是我的 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
我不知道问题出在哪里。有人能帮我吗?
1 个回答
0
你在索引和排序的定义中提到了一个不存在的字段 publish
:
class Meta:
ordering = ['-publish']
# ^^^^^^^
indexes = [ models.Index(fields=['-publish']),]
# ^^^^^^^
你可能是想提到 published
这个字段。