我是 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
我不知道问题所在。有人可以帮助我吗?
您在索引和排序定义中引用了不存在的字段
publish
:
class Meta:
ordering = ['-publish']
# ^^^^^^^
indexes = [ models.Index(fields=['-publish']),]
# ^^^^^^^
您可能想引用
published
字段