Django: How to change a field widget in a Inline Formset

后端 未结 4 1032
别那么骄傲
别那么骄傲 2021-02-13 07:10

I am new to Django and I think I am missing this in the docs.
The problem is that in inline-formset I dont declare a form, just pass two models to construct it.
I want

4条回答
  •  Happy的楠姐
    2021-02-13 07:57

    This is an example of customizing one field using formfield_callback:

    def formfield_callback(field):
        if isinstance(field, models.ChoiceField) and field.name == 'target_field_name':
            return fields.ChoiceField(choices = SAMPLE_CHOICES_LIST, label='Sample Label')
        return field.formfield()
    
    FormSet = inlineformset_factory(ModelA, ModelB, extra=1, formfield_callback = formfield_callback)
    

提交回复
热议问题