11

I have a "Person" Model which has a one-to-many Relations to other Models, for instance Address. I want to edit these models on the same page as Person, which I can already do via inlines. But I also want to change the order of the fields.

I want the (inline) field "Address" to be the third item in the list, but for

fields ('first_name', 'last_name', 'Adress_Inline', 'nationality' etc..) I get this: PersonAdmin.fields' refers to field 'Address_Inline' that is missing from the form.

Is there a way to change the order of the fields and get certain inline-Fields between 'regular' Model fields?

Thank You!

4 Answers4

2

User django fieldsets to define fields order for view and add/edit view of object.

class YourCustomClass(admin.ModelAdmin):

    models = Your_Model
    fieldsets = ((None, {'fields': ('image', 'name',)}),)

    add_fieldsets = ((None, {'fields': ('name', 'image',)}),)
SuReSh
  • 1,503
  • 1
  • 22
  • 47
  • While somewhat useful, this is misleadning and does not really answer the original question, as [fieldsets](https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.fieldsets) cannot be used to order InlineAdmins. – vlz Nov 30 '22 at 16:14
0

You would have to manually extend the admin template (change_form.html) and hard-code positions into it. AFAIK, you cannot insert inline formsets into the middle of a parent form.

Jeff Ober
  • 4,967
  • 20
  • 15
0

Whenever Django render admin form template, list of inline fields are available in a variable {{ inline_admin_formset }}, you can use this to show inline fields anywhere in your own custom change_form.html template.

You must extend change_form.html template in order to do that, there is no other way.

S Saini
  • 11
  • 2
-3

You can do that with javascript. You need to override the template and maybe add some div tag. Then hardcode your div's that you want copied and then hide the original div (with the inline). I hope that helps.

orwellian
  • 388
  • 4
  • 15