Skip to main content
 首页 » 编程设计

python之Django 迁移错误 : ERRORS: 'choices' must be an iterable (e. g.,列表或元组)

2025年05月04日35lovecherry

我已经为博客应用程序创建了模型。这是我的模型.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) 

谁能帮我解决这个问题?

请您参考如下方法:

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

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