Django迁移错误:错误:“选择”必须是可迭代的(例如,列表或元组)

时间:2017-10-22 09:06:18

标签: python django django-models django-admin django-migrations

我为博客应用程序创建了模型。这是我的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):
    STATUS_CHOICE=(
        ('draft','DRAFT'),
        ('published','Published'),
    )

    title=models.CharField(max_length=250)
    slug=models.SlugField(max_length = 250,unique_for_date='publish')
    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

当我尝试迁移模型时,我收到错误:

ERRORS:
myblog.post.status: (fields.E004) 'choices' must be an iterable (e.g., a list or tuple).

这是我的admin.py文件:

from django.contrib import admin
from .models import post

# Register your models here.
admin.site.register(post)

请有人帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:5)

choices需要引用您上面声明的列表,而不是字符串:

status = models.CharField(max_length=10,
                            choices = STATUS_CHOICE,
                                default='draft')

答案 1 :(得分:0)

请从

中的状态选项中删除引号
status = models.CharField(max_length=10,
                        choices = 'STATUS_CHOICES',
                            default='draft')

TO:

status = models.CharField(max_length=10,
                        choices = STATUS_CHOICE,
                            default='draft')