Custom form fields in admin site

26 views
Skip to first unread message

Luis Zárate

unread,
Sep 8, 2016, 12:41:54 PM9/8/16
to django...@googlegroups.com
Hi,  I am trying to do something like


class MyForm(forms.ModelForm):
     myextra_field = forms.IntegerField()

    class Meta:
          models = Mymodel
          fields = '__all__'

class MymodelAdmin(admin.ModelAdmin):
      form = MyForm
      fields = ["name", 'myextra_field']

    def save(self, *args, **kwargs)
        do something with myextra_field


I know that is possible to specify readonly_fields and fields works but in readonly mode that is not what i want.

So is there any way to have extra form fields in admin.ModelAdmin in edit mode?



--
"La utopía sirve para caminar" Fernando Birri


ludovic coues

unread,
Sep 9, 2016, 4:02:33 AM9/9/16
to django...@googlegroups.com
It's not easy to understand what you are trying to do and in which way it is failing. It would require us setting up a test project.
I am assuming you want that the Admin display a form with a field for each of Mymodel field plus the myextra_field attribute and what you get is either only the name fields and maybe the myextra_field. If that not the case, plus give us more information about your problem.

MymodelAdmin.fields is overriding MyForm.fields. If you are curious, it's around line 594 of django/contrib/admin/options.py. If fields is set, get_fields return that. Else, it will return the form base_fields and the modeladmin readonly_fields.



--
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/CAG%2B5VyNVmNPzjkd-9bV_j35%2BNn5PYdjvMzi6GQv%2BhaDFZLrbug%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42

Luis Zárate

unread,
Sep 9, 2016, 8:21:33 PM9/9/16
to django...@googlegroups.com
Hi,

I started to make a test project, and review the django code in options, but I see the problem and started to code in other way  the result is in
Now is more interesting because I try to provide a way to update onetoone fields in the same form.  I made an elegant solution rewriting __new__ function of modelform, but I have son dudes about my implementation and I really need to test it.  (Finished coding a few minutes ago)

In the REAME are the to issues that make me ask for help those are:
  • If I use RelToParentFrom in onetooneForm/forms.py works ok, but I wanted something like in onetooneForm/forms_wanted.py
  • No way to sort fields
  • How can I use formfield_callback? it's necesary





For more options, visit https://groups.google.com/d/optout.



--
Reply all
Reply to author
Forward
0 new messages