Add extra fields to a ModelAdmin form

503 views
Skip to first unread message

James Foley

unread,
Apr 30, 2018, 4:41:21 AM4/30/18
to Django users
So, I have a ModelAdmin that I need to add extra fields to. These fields do not exist on the model, but will be dynamically added to a custom ModelForm through the __init__ method, and logic inside clean will handle the returned data on save.

I can't seem to find any solid information related to adding custom non-model fields to a ModelAdmin form. The closest I have come is by overriding get_fields on the ModelAdmin class and updating self.form.declared_fields with the new fields I'd like to add. 

This just doesn't feel very clean to me and I was curious if there was a better way to add new fields to a ModelAdmin dynamically?

Mark Phillips

unread,
May 2, 2018, 11:02:51 AM5/2/18
to django users
James,

Did you ever get a response to this issue? I am having a devil of a time adding some extra non-model fields to an admin inline form. My added complication is that the fields are only defined at run time, so I have to have a loop that creates them based on a different model in my app. 

Would you be willing to share your code with me? In particular, I am not sure where and how you are using self.form.declared_fields. 

I am running django 1.11.

Thanks!

Mark

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a590e54f-1f36-497b-b508-cba339c5f2fc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

James Foley

unread,
May 2, 2018, 12:12:44 PM5/2/18
to Django users
Hey Mark,

I've not actually found a solution yet, I've just sort of put it at the bottom of my pile of problems...

Here is the post that I used to get where I am https://stackoverflow.com/a/35797311/3173119

It just doesn't feel very Django like, and I feel that there must be an easier way to simply add a non-model field to a model admin form. My fields are also added at run time which is whats causing me issues.

My actual goal is to convert a flat JSON string stored in a JSON field, into fields for each key. Then once saved it coverts them all back into a JSON string and stored them back on the one JSON field.

James.


On Wednesday, 2 May 2018 16:02:51 UTC+1, mark wrote:
James,

Did you ever get a response to this issue? I am having a devil of a time adding some extra non-model fields to an admin inline form. My added complication is that the fields are only defined at run time, so I have to have a loop that creates them based on a different model in my app. 

Would you be willing to share your code with me? In particular, I am not sure where and how you are using self.form.declared_fields. 

I am running django 1.11.

Thanks!

Mark
On Mon, Apr 30, 2018 at 1:41 AM, 'James Foley' via Django users <django...@googlegroups.com> wrote:
So, I have a ModelAdmin that I need to add extra fields to. These fields do not exist on the model, but will be dynamically added to a custom ModelForm through the __init__ method, and logic inside clean will handle the returned data on save.

I can't seem to find any solid information related to adding custom non-model fields to a ModelAdmin form. The closest I have come is by overriding get_fields on the ModelAdmin class and updating self.form.declared_fields with the new fields I'd like to add. 

This just doesn't feel very clean to me and I was curious if there was a better way to add new fields to a ModelAdmin dynamically?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Mark Phillips

unread,
May 5, 2018, 11:57:02 AM5/5/18
to django users
James,

I was finally able to add extra fields to a ModelForm dynamically! Check out the answer to this post - https://stackoverflow.com/questions/32335349/how-do-i-create-and-save-dynamic-fields-in-django-modeladmin

It worked for me in django 2. I have not tried it in django 1.11. 

I hope this help!

Mark

To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages