Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Model Choices

I've been stumped by how to make choices within my models for hours.

So far I've been having issues with my approved field in the model. I want approved to be 1 of the 3 choices,but what I appear to get is a tuple of all three choices. Within './manage.py shell', I get

>>> listing.objects.all()[0].approved
((u'1', u'Awaiting'), (u'2', u'No'), (u'3', u'Yes'))

My Model:

from django.db import models

# Create your models here.
class directory(models.Model):
    name = models.CharField(max_length="50")

class listing(models.Model):
    name = models.CharField(max_length="50")
    directory = models.ForeignKey(directory)
    birthday = models.DateField()
    state = models.CharField(max_length="2") 
    owner = models.CharField(max_length="50")
    approved = (
        (u'1', u'Awaiting'),
        (u'2', u'No'),
        (u'3', u'Yes'),
    )

Also side question: But whenever I make model changes and try to migrate schemas with South my commandline will freeze up and won't ever finish migrating schemas. Any possible suggestions for why it freezes? It can detect changes but wont ever finish implementing them. Because it never finishes, I cant access my model through the admin panel anymore when I click on the model to make changes, I can never load the page.

The order in which I run the commands are
    ./manage.py convert_to_south myapp
    ./manage.py schemamigration southtut --auto
    ./manage.py migrate southtut ( never progresses on this command after the first few lines appear)
like image 798
user1431282 Avatar asked Dec 24 '12 21:12

user1431282


People also ask

What is choices in Django model?

Choices limits the input from the user to the particular values specified in models.py . If choices are given, they're enforced by model validation and the default form widget will be a select box with these choices instead of the standard text field.

What are the fields in Django?

Fields in Django are the data types to store a particular type of data. For example, to store an integer, IntegerField would be used. These fields have in-built validation for a particular data type, that is you can not store “abc” in an IntegerField.

How many types of Django forms are there?

Django form fields define two types of functionality, a form field's HTML markup and its server-side validation facilities.

What is CharField in Django?

CharField is generally used for storing small strings like first name, last name, etc. To store larger text TextField is used. The default form widget for this field is TextInput. CharField has one extra required argument: CharField.max_length.


1 Answers

approved as you have it isn't a field, it's simply a class attribute containing the three choices. The choices need to be an attribute of an actual field:

APPROVAL_CHOICES = (
    (u'1', u'Awaiting'),
    (u'2', u'No'),
    (u'3', u'Yes'),
)
approved = models.CharField(max_length=1, choices=APPROVAL_CHOICES)
like image 108
Daniel Roseman Avatar answered Sep 18 '22 01:09

Daniel Roseman