开发者

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.


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.


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜