Sunday, May 17, 2020

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

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

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

No comments:

Post a Comment