How to change a selection in a Django model?

I have a Django model that uses the choices attribute .

 COLOR_CHOICES = ( ('R', 'Red'), ('B', 'Blue'), ) class Toy(models.Model): color = models.CharField(max_length=1, choices=COLOR_CHOICES) 

My code is working, and now I would like to add additional options.

 COLOR_CHOICES = ( ('R', 'Red'), ('B', 'Blue'), ('G', 'Green'), ) 

How can I do it? Does Django use database restrictions to provide choices? Do I need to perform database migration (I use South )? Or is Django just applying a selection constraint in Python code, and all I have to do is change the code and restart it?

Thanks!

+7
source share
2 answers

Django does not apply database-level selections, it only uses them for widget presentation and validation. If you want them to be a bit more "dynamic", for example, to have different ones on different servers, you could define them through settings.py :

 from django.conf import settings COLOR_CHOICES = getattr(settings, 'COLOR_CHOICES',( ('R', 'Red'), ('B', 'Blue'), ('G', 'Green'), )) 

You can then define the various options in settings.py (no database migration needed!).

+11
source

The models.CharField field, so the database will process it like any other models.CharField installed in django :)

No, it does not provide a choice / you do not need to touch your database.

+1
source

All Articles