Django模型中的动态选择字段

时间:2021-11-21 02:05:11

My models.py:

我的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.

在这里,如果Feed.shop =='shop1',我想在Feed.location上加载SHOP1_CHOICES。目前无论什么商店,它只显示SHOP1_CHOICES(毫不奇怪)。我如何实现它?我被卡住了,请帮忙。

4 个解决方案

#1


14  

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

#2


6  

From the Django docs: http://docs.djangoproject.com/en/dev/ref/models/fields/#choices

来自Django文档: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.

最后,请注意,选择可以是任何可迭代对象 - 不一定是列表或元组。这使您可以动态构造选择。但是如果你发现你自己选择了动态的选择,你可能最好使用一个带有ForeignKey的正确数据库表。选择适用于静态数据,如果有的话,变化不大。

#3


5  

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

#4


-7  

You need to use some Ajax functionality. There's no way that I know where you can do it in standard django admin interface without hacking the admin CSS, templates etc.

您需要使用一些Ajax功能。我无法在标准的django管理界面中知道你在哪里可以做到这一点而不会破坏管理CSS,模板等。

I would recommend you to use some kind of cascading library, to implement this functionality in your own custom views using ModelForms.

我建议您使用某种级联库,使用ModelForms在您自己的自定义视图中实现此功能。

I have done the same with jquery plugin Cascade in a few cases.There are better implementations, but this also works fine. See link below

在一些情况下,我已经使用jquery插件Cascade完成了相同的操作。有更好的实现,但这也可以正常工作。见下面的链接

http://plugins.jquery.com/project/cascade

http://plugins.jquery.com/project/cascade

#1


14  

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

#2


6  

From the Django docs: http://docs.djangoproject.com/en/dev/ref/models/fields/#choices

来自Django文档: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.

最后,请注意,选择可以是任何可迭代对象 - 不一定是列表或元组。这使您可以动态构造选择。但是如果你发现你自己选择了动态的选择,你可能最好使用一个带有ForeignKey的正确数据库表。选择适用于静态数据,如果有的话,变化不大。

#3


5  

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

#4


-7  

You need to use some Ajax functionality. There's no way that I know where you can do it in standard django admin interface without hacking the admin CSS, templates etc.

您需要使用一些Ajax功能。我无法在标准的django管理界面中知道你在哪里可以做到这一点而不会破坏管理CSS,模板等。

I would recommend you to use some kind of cascading library, to implement this functionality in your own custom views using ModelForms.

我建议您使用某种级联库,使用ModelForms在您自己的自定义视图中实现此功能。

I have done the same with jquery plugin Cascade in a few cases.There are better implementations, but this also works fine. See link below

在一些情况下,我已经使用jquery插件Cascade完成了相同的操作。有更好的实现,但这也可以正常工作。见下面的链接

http://plugins.jquery.com/project/cascade

http://plugins.jquery.com/project/cascade