Django ModelChoiceField - use something other than id?

后端 未结 3 1622
礼貌的吻别
礼貌的吻别 2021-02-14 04:07

Say I have an address table and it has a postal_code field -- ModelChoiceField does not allow me to use something other than PKs to validate existence

3条回答
  •  感动是毒
    2021-02-14 04:39

    What about to_field_name? I'm not sure if it's documented anywhere, but you can find it easily between ModelChoiceField constructor params: https://github.com/django/django/blob/master/django/forms/models.py. It is used to filter field queryset.

    For example:

    articles = ModelChoiceField(queryset=Articles.objects.all(),
            to_field_name='slug')
    

提交回复
热议问题