![](/img/trans.png)
[英]How to check if there's any odd/even numbers in an Iterable (e.g. list/tuple)?
[英]Django Migration Error: ERRORS: 'choices' must be an iterable (e.g., a list or tuple)
我為博客應用程序創建了模型。 這是我的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)
請問有人能幫我解決這個問題嗎?
choices
需要參考您在上面聲明的列表,而不是字符串:
status = models.CharField(max_length=10,
choices = STATUS_CHOICE,
default='draft')
請從 STATUS CHOICES 中刪除引號
status = models.CharField(max_length=10,
choices = 'STATUS_CHOICES',
default='draft')
致:
status = models.CharField(max_length=10,
choices = STATUS_CHOICE,
default='draft')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.