Nazim Zeeshan
Nazim Zeeshan

Reputation: 733

Dynamic choices field in Django Models

My models.py:

SHOP1_CHOICES = (
    ('Food Court', 'Food Court'),
    ('KFC', 'KFC'),

)

SHOP2_CHOICES = (
    ('Sports Arena', 'Sports Arena'),
    ('Disco D', 'Disco D'),

)

SHOP3_CHOICES = (
    ('Bowling Arena', 'Bowling Arena'),
    ('Cinemax', 'Cinemax'),

)

class Feed(models.Model):
  gender = models.CharField(max_length=5, choices=GENDER_CHOICES, default='girl')
  name =models.CharField(max_length=25)
  shop=models.CharField(max_length=20)
  location=models.CharField(max_length=25, choices=SHOP1_CHOICES)

Here if Feed.shop == 'shop1' I want to load SHOP1_CHOICES on Feed.location. Currently irrespective of what shop, it just displays the SHOP1_CHOICES (no surprise).How can I implement it? I am stuck, please help.

Upvotes: 21

Views: 34971

Answers (3)

dani herrera
dani herrera

Reputation: 51715

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)()

Upvotes: 26

ojno
ojno

Reputation: 430

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.

Upvotes: 27

Willian
Willian

Reputation: 2445

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()

Upvotes: 7

Related Questions