BUT when I use `InlineModelAdmin.get_fieldsets`, I receive parent object.
In my example that would be -- when I call
`AccountInline.get_fieldsets` -- I receive `Customer` object instead of
`Account. That prevents me from configuring formsets to the account I
have.
If I received `Account`, I could extract `Customer` from it. But if I
receive `Customer`, I cannot adapt form for each row.
Also, it seems strange that class explicitly linked to `Account` receives
`Customer` object as input.
Code:
{{{
# in models.py
class Customer(models.Model):
num = models.IntegerField()
class Account(models.Model):
ACCOUNT_TYPES = (
(1, 'A'),
(2, 'B'),
)
customer = models.ForeignKey(Customer)
account_type = models.IntegerField(choices=ACCOUNT_TYPES)
a = models.CharField(max_length=255, blank=True) # should be edited
when type = "A"
b = models.CharField(max_length=255, blank=True) # should be edited
when type = "B"
# in admin.py
class AccountInline(admin.TabularInline):
model = models.Account
def get_fieldsets(self, request, obj=None):
print(repr(obj)) # => <Customer>
"""
if obj and obj.account_type == 1:
return ((None, {'fields': ('account_type', 'a')}),)
elif obj and obj.account_type == 2:
return ((None, {'fields': ('account_type', 'b')}),)
"""
return ((None, {'fields': ('account_type',)}),)
@admin.register(models.Customer)
class CustomerAdmin(admin.ModelAdmin):
inlines = (AccountInline,)
@admin.register(models.Account)
class AccountAdmin(admin.ModelAdmin):
def get_fieldsets(self, request, obj=None):
print(repr(obj)) # => <Account>
if obj and obj.account_type == 1:
return ((None, {'fields': ('account_type', 'a')}),)
elif obj and obj.account_type == 2:
return ((None, {'fields': ('account_type', 'b')}),)
return ((None, {'fields': ('account_type',)}),)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28831>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Comment (by tonnzor):
By the way, custom fields on inline admins receive correct object:
{{{
class AccountInline(admin.TabularInline):
...
def type_verbose(self, obj):
return "Account is {}".format(obj.get_account_type_display())
type_verbose.short_description = "Account type"
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28831#comment:1>
* component: contrib.admin => Documentation
* type: Bug => Cleanup/optimization
* stage: Unreviewed => Accepted
Comment:
I don't believe changing the behavior is possible and of course it would
also be backwards incompatible. Perhaps the documentation could
[https://github.com/django/django/blob/3f237c1a5b936a9b85304cffbf3343f491e395d6/django/contrib/admin/options.py#L1428-L1439
clarify the current behavior].
--
Ticket URL: <https://code.djangoproject.com/ticket/28831#comment:2>
* cc: Manel Clos (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/28831#comment:3>
* owner: nobody => Parth Patil
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/28831#comment:4>
* has_patch: 0 => 1
* version: 2.0 => master
Comment:
[https://github.com/django/django/pull/11431 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/28831#comment:5>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"85436473066aae5d793caeced010ea4f45da4e79" 8543647]:
{{{
#!CommitTicketReference repository=""
revision="85436473066aae5d793caeced010ea4f45da4e79"
Fixed #28831 -- Doc'd that InlineModelAdmin methods' obj argument is the
parent object.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28831#comment:6>
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):
In [changeset:"5c85244eb907974e9bed380df57458640125368a" 5c85244]:
{{{
#!CommitTicketReference repository=""
revision="5c85244eb907974e9bed380df57458640125368a"
[2.2.x] Fixed #28831 -- Doc'd that InlineModelAdmin methods' obj argument
is the parent object.
Backport of 85436473066aae5d793caeced010ea4f45da4e79 from master
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28831#comment:7>