how to add an extra field in an admin form (not part of the model)

384 views
Skip to first unread message

Carles Pina i Estany

unread,
May 17, 2020, 9:21:10 AM5/17/20
to django...@googlegroups.com

Hi,

Last week I tried to do something in admin but it didn't work as I had guessed.

The simplification of what I wanted is to have an extra field in the admin
form.

What I tried to do is what I do in forms outside admin:

---------
class MyModelForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['extra_field'] = forms.CharField(max_length=100)

def save(self, *args, **kwargs):
# here I would like to have some code using the new extra_field
pass

class Meta:
model = MyModel
fields = ['amount', 'project']

class MyModelAdmin(admin.ModelAdmin):
form = MyModelForm
---------

The "extra_field" is not displayed in the "Add MyModel" form in the admin.

After this I tried different approaches too long to explain here (I mean, all
failed in different ways).

Does any of you expand an admin form with a new field? any idea, references,
etc. welcomed!

Thank you very much,

--
Carles Pina i Estany

Anonymous Patel

unread,
May 17, 2020, 9:23:40 AM5/17/20
to django...@googlegroups.com
https://youtu.be/lHQI9ydQlSU

Try this video it might help, it helped me for the same problem.

Raj Patel

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/20200517111312.GA23248%40pina.cat.

Carles Pina i Estany

unread,
May 17, 2020, 10:09:35 AM5/17/20
to django...@googlegroups.com

Thanks very much! I've quickly watched the video... sadly the video
helps to add fields into the registration form, not in the admin section
(the, by default, /admin URL).

As a side note and for future reference anyone here: one of my attempts
on Friday was following this one:
https://gist.github.com/riklomas/511440

This uses exactly my first approach. And it works. But only because the
default models.User already has an "email" field (not displayed by
default in http://localhost:8000/admin/auth/user/add/ )

And in a "desperate" way I realised also that UserAdmin overrides
get_form and this works:
https://stackoverflow.com/questions/54262075/difference-between-add-form-and-form

I did "MyAdminForm" doing the same as Django in the UserAdmin but this
only works if the model has the field to be added (this was sad, I
realised too late that models.User has it!)

Anyway, if anyone has added a field in a model form that doesn't belong
to the model: any pointers, help, ideas are welcomed!

Thanks for any ideas!

Carles
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAF_9Cit_cQ2ErCKsKNe4TgVxF%2BAcjatq0fYgzOyf5P0cDD7Skg%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages