Inline formset in Django - removing certain fields

前端 未结 3 970
暖寄归人
暖寄归人 2021-01-31 23:10

I need to create an inline formset which

a) excludes some fields from MyModel being displayed altogether

b) displays some some fields MyMode

3条回答
  •  后悔当初
    2021-01-31 23:45

    I just had a similar issue (not for admin - for the user-facing site) and discovered you can pass the formset and fields you want displayed into inlineformset_factory like this:

    factory = inlineformset_factory(UserProfile, PointTransaction, 
                    formset=PointTransactionFormset,
                    fields=('description','points_type'))
    formset = factory(instance=user_profile, data=request.POST)
    

    where user_profile is a UserProfile.

    Be warned that this can cause validation problems if the underlying model has required fields that aren't included in the field list passed into inlineformset_factory, but that's the case for any kind of form.

提交回复
热议问题