Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 943
Nazim Zeeshan Avatar asked May 14 '11 13:05

Nazim Zeeshan


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 is Django dynamic?

dynamic-django-forms is a simple, reusable app that allows you to build (and respond to) dynamic forms, i.e. forms that have variable numbers and types of fields. A few examples of uses include: Building and sending out surveys. Job applications where each job might have a different application forms.

What does field type signifies in Django models?

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. Similarly, for other fields.


2 Answers

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)()
like image 161
dani herrera Avatar answered Sep 22 '22 23:09

dani herrera


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.

like image 43
ojno Avatar answered Sep 22 '22 23:09

ojno