我是Python和Django的新手。在书上尝试一个例子时,我做了本书所说的内容并创建了一个模型文件,如下所示
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
class Post(models.Model):
STATUS_CHOICES = (
('draft','Draft'),
('published','Published'),
)
title = models.CharField(max_length=250)
slug = models.SlugField(max_length=250,
unique_for_date='published')
author = models.ForeignKey(User,
related_name='blog_posts')
body = models.TextField()
publish = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=10,
choices = STATUS_CHOICES,
default='draft')
class Meta:
ordering = ('-publish',)
def __str__(self):
return self.title
当我运行命令python manage.py makemigrations blog
它说NameError: name 'STATUS_CHOICES' is not defined
我从书中输入完全正确,我无法运行此命令
答案 0 :(得分:0)
尝试缩进代码,你必须尊重编码风格:
status = models.CharField(max_length=10,
choices=STATUS_CHOICES,
default='draft')
答案 1 :(得分:0)
更改
choices = STATUS_CHOICES,
到
choices = 'STATUS_CHOICES',
您遗失''
。
答案 2 :(得分:0)
这是缩进和间距的问题,无论如何在发布字段中添加默认值并对其进行代码格式化后仍然有效。
model.py 托管在here中,并且 DONOT 可以像编辑器一样在记事本中进行编辑或打开,否则它将再次产生间距,只需在IDE中导入< / p>