简体   繁体   English

Django - 如何创建依赖选择

[英]Django - How to create dependent selects

My task is to implement a form in which the choice of the value of the second field depends on the value of the first field.我的任务是实现一个表单,其中第二个字段的值的选择取决于第一个字段的值。 (For example, if the value of the first field is Cars, then the second field should show sedan/SUV, etc., if the value of the first field is Commercial vehicles, then the second box should show truck/bus, etc.) (例如,如果第一个字段的值为 Cars,则第二个字段应显示轿车/SUV 等,如果第一个字段的值为商用车,则第二个框应显示卡车/公共汽车等。 )

Code models.py :代码models.py

class TypeTransport(models.Model):
    transport_name = models.CharField(max_length=100, verbose_name='kind of transport')


class TypeBodyTransport(models.Model):
    transport = models.ForeignKey(TypeTransport, on_delete=models.CASCADE, blank=True, null=True,
                                  verbose_name='kind of transport')
    body_name = models.CharField(max_length=100, verbose_name='transport body type')


class Advertisement(models.Model):
    transport = models.ForeignKey(TypeTransport, on_delete=models.SET_NULL, blank=True, null=True,
                                  verbose_name='kind of transport')
    body = models.ForeignKey(TypeBodyTransport, on_delete=models.SET_NULL, blank=True, null=True,
                             verbose_name='transport body type ')

Code forms.py :代码forms.py

class CreateAdvertisementForm(forms.ModelForm): 
    transport = forms.ModelChoiceField(queryset=TypeTransport.objects.all(), to_field_name="transport_name")
    body = forms.ModelChoiceField(queryset=TypeBodyTransport.objects.filter(transport=transport),
                                  to_field_name="body_name")
    class Meta:
        model = Advertisement
        fields = ('transport', 'body')

I thought it could be done with filter(transport=transport) , but this error is returned: TypeError: Field 'id' expected a number but got <django.forms.models.ModelChoiceField object at 0x7f40d7af5ac0>.我认为可以使用filter(transport=transport)来完成,但返回此错误: TypeError: Field 'id' expected a number but got <django.forms.models.ModelChoiceField object at 0x7f40d7af5ac0>.

Can you please tell me how to implement the feature I need?你能告诉我如何实现我需要的功能吗?

have you tried:你有没有尝试过:

class CreateAdvertisementForm(forms.ModelForm): 
    transport = forms.ModelChoiceField(queryset=TypeTransport.objects.all(), to_field_name="transport_name")
    body = forms.ModelChoiceField(queryset=TypeBodyTransport.objects.filter(transport=transport.id),
                                  to_field_name="body_name")
    class Meta:
        model = Advertisement
        fields = ('transport', 'body')

instead of transport = transport , try transport = transport.id代替transport = transport ,尝试transport = transport.id

I solved this problem using the django-smart-selects library.我使用django-smart-selects库解决了这个问题。 Additionally I can say that in the forms it is necessary to remove field which references to the ModelChoiceField , because it interferes with its queryset parameter.另外我可以说,在queryset中,有必要删除引用ModelChoiceField的字段,因为它会干扰其查询集参数。 I'm still a beginner, so I didn't immediately guess that the problem was in chained selects (I edited the question).我仍然是初学者,所以我没有立即猜测问题出在链式选择中(我编辑了问题)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM