简体   繁体   English

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

[英]Django Migration Error: ERRORS: 'choices' must be an iterable (e.g., a list or tuple)

I have created models for a blog application.我为博客应用程序创建了模型。 This is my models.py:这是我的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

When I tried to migrate models I'm getting the error:当我尝试迁移模型时,出现错误:

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

Here is my admin.py file:这是我的 admin.py 文件:

from django.contrib import admin
from .models import post

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

Please can anyone help me to solve this issue?请问有人能帮我解决这个问题吗?

choices need to refer to the list you've declared above, not a string : choices需要参考您在上面声明的列表,而不是字符串:

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

Please remove the quotes from STATUS CHOICES in请从 STATUS CHOICES 中删除引号

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

TO:致:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查Iterable中是否有任何奇数/偶数(例如列表/元组)? - How to check if there's any odd/even numbers in an Iterable (e.g. list/tuple)? 在 PySimpleGUI 中创建 window 布局时出现错误“您的行不是可迭代的(例如列表)” - Error "Your row is not an iterable (e.g. a list)" when creating window layout in PySimpleGUI 使用容器(例如元组或列表)进行 Numpy 切片 - Numpy slicing with container (e.g. tuple or list) 如何在django模型中修复“选择必须是可迭代的”错误? - How to fix 'choices must be an iterable' error in django models? 从可迭代对象(例如列表或集合)生成伪有序对列表的Python方法 - Pythonic way to generate a pseudo ordered pair list from an iterable (e.g. a list or set) Django Admin错误admin.E008字段集的值必须是列表或元组 - Django Admin error admin.E008 The value of fieldsets must be a list or tuple 为什么分配给空列表(例如 [] = "")不是错误? - Why isn't assigning to an empty list (e.g. [] = "") an error? 如何找出 django 南迁移中的数据库(例如是否使用 mysql/pgsql/?)? - How to find out what database (e.g. wether using mysql/pgsql/?) in a django south migration? 从 Django 中的模型类生成可迭代的选择元组 - Generate iterable choices tuple from model class in Django 选择必须是可迭代的 - Choices must be iterable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM