1

I've been looking on the documentation and stackoverflow/forums for a way to ignore the inline children of a model when I save it in django admin. I've been searching for a few days and I can't seem to find an answer.

I have a normal tabularinline object:

class UserOrdersAdmin(admin.TabularInline):
    model = Order
    classes = ['collapse']

And a normal User admin registration:

class UserAdmin(BaseUserAdmin):
    inlines = (UserOrdersAdmin, UserSettingsAdmin)

admin.site.unregister(User)
admin.site.register(User, UserAdmin)

What I simply want is when I press save within the User "change view", it will ignore the inline "UserOrderAdmin" which is inline to UserAdmin.

  • 1
    From the question it is not entirely clear what you are trying to achieve. But I think you should look at the "extra", "max_num" and "readonly_fields" fields you can set on the Inline. Also take a look at the other fields: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#inlinemodeladmin-options – jgadelange Jul 06 '17 at 13:32
  • 1
    @jgadelange well I put all of the fields in the readonly_fields in UserOrderAdmin and it effectively does the job. However it really don't do what I wanted, which was to only save Parent, as it's most likely still trying to save the UserOrder at the same time. I was hoping there was some easy way of making the entire inline model stop saving on parent save. If I add a new field to the model I have to go in and add it here as well. If you want to make it a proper answer so I can accept it? – Andreas Halvorsen Tollånes Jul 06 '17 at 13:42

2 Answers2

2

From your response on my comment I am getting the idea you want to show some extra information in the admin, that is not editable. This can be achieved using readonly_fields in the Inline, for completeness you should also set the max_num to 0, because otherwise you can add empty inlines.

You could enter all fields manually or use something like given in this answer: https://stackoverflow.com/a/42877484/2354734

The end result would look something like this.

class UserOrdersAdmin(admin.TabularInline):
    model = Order
    classes = ['collapse']
    max_num = 0

    def get_readonly_fields(self, request, obj=None):
        return list(set(
            [field.name for field in self.opts.local_fields] +
            [field.name for field in self.opts.local_many_to_many]
        ))

For completeness of the answer also a link to the documentation

jgadelange
  • 2,482
  • 1
  • 13
  • 10
0

Try this:

class UserOrdersAdmin(admin.TabularInline):
    model = Order
    classes = ['collapse']
    extra = 0
Diego Puente
  • 1,964
  • 20
  • 23