条件/偶然模型领域的选择 [英] conditional/contingent model field choices
问题描述
如果我有一个模型有两个模型选择字段,有没有办法使第二个字段的选择集取决于第一个选择的内容。例如,如果给出以下代码,用户为公司字段选择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屋!