django admin - add custom form fields that are not part of the model

后端 未结 5 1216
无人及你
无人及你 2020-11-30 18:07

I have a model registered in the admin site. One of its fields is a long string expression. I\'d like to add custom form fields to the add/update page of this model in the a

5条回答
  •  无人及你
    2020-11-30 19:06

    If you absolutely only want to store the combined field on the model and not the two seperate fields, you could do something like this:

    • Create a custom form using the form attribute on your ModelAdmin (https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.form)
    • Parse the custom fields in the save_formset method on your ModelAdmin (https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_model)

    I never done something like this so I'm not completely sure how it will work out.

提交回复
热议问题