条件/偶然模型领域的选择 [英] conditional/contingent model field choices

查看:119
本文介绍了条件/偶然模型领域的选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个模型有两个模型选择字段,有没有办法使第二个字段的选择集取决于第一个选择的内容。例如,如果给出以下代码,用户为公司字段选择APPLE - 可以配置代码,以便用户仅显示分割字段选择的APPLE DIV_CHOICES。

  CMP_CHOICES('Apple','Apple Computers'),
('MS','Microsoft Inc.'),

APPLE DIV_CHOICES('Desktop','Desktop'),
('iOS','iOS'),
('AS','AppStore'),

MS DIV_CHOICES('Windows','Windows'),
('Longhorn','Longhorn'),
('Mobile','Mobile'),

class Contact (models.Model)
first_name = models.CharField(max_length = 64,...)
last_name = models.CharField(max_length = 64,...)
company = models.CharField(max_length = 100 ,choices = CMP_CHOICES)
division = models.CharField(max_length = 100,choices = DIV_CHOICES)
....


解决方案

没有。选择必须是所有可能的选择。但是,您可以使用表单上的JavaScript来限制基于第一选择的选择,但不限于模型本身。


if I have a model that has two model choice fields, is there a way to make the choice set of the second field dependent on what is chosen in the first. Example, if given the following code, the User chooses APPLE for the "company" field — can the code be configured such that the User is presented ONLY the APPLE DIV_CHOICES for his "division" field choices?

CMP_CHOICES ('Apple', 'Apple Computers'),
            ('MS', 'Microsoft Inc.'),

APPLE DIV_CHOICES ( 'Desktop', 'Desktop'),
          ( 'iOS', 'iOS'),
          ( 'AS', 'AppStore'),

MS DIV_CHOICES ( 'Windows', 'Windows'),
           ( 'Longhorn', 'Longhorn'),
           ( 'Mobile', 'Mobile'),

class Contact(models.Model)
first_name = models.CharField(max_length=64, …)
last_name = models.CharField(max_length=64, …)
company = models.CharField(max_length=100, choices=CMP_CHOICES)
division = models.CharField(max_length=100, choices=DIV_CHOICES)
....

解决方案

No. Choices must be all choices that are ever possible. However, you may use JavaScript on your form to limit the choices based on the first selection, but not on the model itself.

这篇关于条件/偶然模型领域的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆