Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django admin inline - show only when changing object

I have a UserProfile which has many required (not null but blank) fields and I'm trying not to show it when adding a user.

I tried multiple things, for example:

def get_formsets_with_inlines(self, request, obj=None):
    if not obj:
        return []
    return super().get_formsets_with_inlines(request, obj)

But after saving User, django raises error which says that fields from UserProfile can't be null.

Do you know how to make it work?

like image 326
Milano Avatar asked Oct 17 '25 10:10

Milano


1 Answers

As of Django 3.0, there is a ModelAdmin.get_inlines() method, which you can override like this:

def get_inlines(self, request, obj=None):
    if obj:
        return [FirstInline, SecondInline]
    else:
        return []

See Django Documentation.

like image 144
Webucator Avatar answered Oct 19 '25 23:10

Webucator



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!