Mounikesh Mintu
Mounikesh Mintu

Reputation: 43

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:

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:

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?

Upvotes: 2

Views: 4062

Answers (2)

rajkris
rajkris

Reputation: 1793

Please remove the quotes from STATUS CHOICES in

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

TO:

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

Upvotes: 2

PRMoureu
PRMoureu

Reputation: 13327

choices need to refer to the list you've declared above, not a string :

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

Upvotes: 5

Related Questions