Sunday, May 17, 2020

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

https://youtu.be/lHQI9ydQlSU

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

Raj Patel

On Sun, 17 May, 2020, 6:50 pm Carles Pina i Estany, <carles@pina.cat> wrote:

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.

--
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/CAF_9Cit_cQ2ErCKsKNe4TgVxF%2BAcjatq0fYgzOyf5P0cDD7Skg%40mail.gmail.com.

No comments:

Post a Comment