27👍
From the Django docs: http://docs.djangoproject.com/en/dev/ref/models/fields/#choices
Finally, note that choices can be any iterable object — not necessarily a list or tuple. This lets you construct choices dynamically. But if you find yourself hacking choices to be dynamic, you’re probably better off using a proper database table with a ForeignKey. choices is meant for static data that doesn’t change much, if ever.
24👍
Django >= 5
More options for declaring field choices
Django 5.0 adds support for accepting a mapping or a callable instead of an iterable, and also no longer requires .choices to be used directly to expand enumeration types:
def get_scores():
return [(i, str(i)) for i in range(10)]
class Winner(models.Model):
name = models.CharField(...)
medal = models.CharField(..., choices=Medal) # Using `.choices` not required.
sport = models.CharField(..., choices=SPORT_CHOICES)
score = models.IntegerField(choices=get_scores) # A callable is allowed.
Previous versions
This is my approach:
I use lazy for lazy load:
from django.utils.functional import lazy
Here, a helper to chose options:
def help_SHOP_CHOICES():
SHOP1_CHOICES = [
('Food Court', 'Food Court'),
('KFC', 'KFC'),
]
SHOP3_CHOICES = [
('Bowling Arena', 'Bowling Arena'),
('Cinemax', 'Cinemax'),
]
return random.choice( SHOP1_CHOICES + SHOP3_CHOICES ) # choose one
Finally the model with dynamic choices:
class Feed(models.Model):
...
location=models.CharField(max_length=25, choices=SHOP1_CHOICES)
def __init__(self, *args, **kwargs):
super(Feed, self).__init__(*args, **kwargs)
self._meta.get_field('location').choices = \
lazy(help_SHOP_CHOICES,list)()
- [Django]-Django admin file upload with current model id
- [Django]-Get count of related model efficiently in Django
- [Django]-Warning: Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'
7👍
I don’t think you should do this on the model, form is a better place. Or you should rethink your models. For example:
class Location(models.Model):
pass
class Shop(models.Model):
location = models.ForeignKey(Location)
class Feed(models.Model):
shop = models.ForeignKey()
- [Django]-Name '_' is not defined
- [Django]-Equivalent of PHP "echo something; exit();" with Python/Django?
- [Django]-Django migration fails with "__fake__.DoesNotExist: Permission matching query does not exist."