[Django] #28831: InlineModelAdmin.get_fieldsets should receive own object, not parent

28 views
Skip to first unread message

Django

unread,
Nov 22, 2017, 10:53:05 AM11/22/17
to django-...@googlegroups.com
#28831: InlineModelAdmin.get_fieldsets should receive own object, not parent
-----------------------------------------+------------------------
Reporter: tonnzor | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 2.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
When I use `ModelAdmin.get_fieldsets`, I receive current object. In my
example that would be -- when I call `AccountAdmin.get_fieldsets` -- I
receive `Account`. That enable me to generate different field sets for
different cases.

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.

Django

unread,
Nov 22, 2017, 11:14:16 AM11/22/17
to django-...@googlegroups.com
#28831: InlineModelAdmin.get_fieldsets should receive own object, not parent
-------------------------------+--------------------------------------

Reporter: tonnzor | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 2.0
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

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>

Django

unread,
Nov 22, 2017, 5:53:21 PM11/22/17
to django-...@googlegroups.com
#28831: Document that InlineModelAdmin methods' obj argument is the parent object
--------------------------------------+------------------------------------
Reporter: tonnzor | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 2.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Tim Graham):

* 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>

Django

unread,
Jan 16, 2018, 6:12:23 AM1/16/18
to django-...@googlegroups.com
#28831: Document that InlineModelAdmin methods' obj argument is the parent object
--------------------------------------+------------------------------------
Reporter: tonnzor | Owner: nobody

Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 2.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Manel Clos):

* cc: Manel Clos (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/28831#comment:3>

Django

unread,
May 19, 2019, 10:46:23 AM5/19/19
to django-...@googlegroups.com
#28831: Document that InlineModelAdmin methods' obj argument is the parent object
-------------------------------------+-------------------------------------
Reporter: Artem Skoretskiy | Owner: Parth
Type: | Patil
Cleanup/optimization | Status: assigned

Component: Documentation | Version: 2.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Parth Patil):

* owner: nobody => Parth Patil
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/28831#comment:4>

Django

unread,
May 31, 2019, 2:17:50 AM5/31/19
to django-...@googlegroups.com
#28831: Document that InlineModelAdmin methods' obj argument is the parent object.

-------------------------------------+-------------------------------------
Reporter: Artem Skoretskiy | Owner: Parth
Type: | Patil
Cleanup/optimization | Status: assigned
Component: Documentation | Version: master

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* 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>

Django

unread,
May 31, 2019, 7:21:42 AM5/31/19
to django-...@googlegroups.com
#28831: Document that InlineModelAdmin methods' obj argument is the parent object.
-------------------------------------+-------------------------------------
Reporter: Artem Skoretskiy | Owner: Parth
Type: | Patil
Cleanup/optimization | Status: closed
Component: Documentation | Version: master
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* 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>

Django

unread,
May 31, 2019, 7:22:08 AM5/31/19
to django-...@googlegroups.com
#28831: Document that InlineModelAdmin methods' obj argument is the parent object.
-------------------------------------+-------------------------------------
Reporter: Artem Skoretskiy | Owner: Parth
Type: | Patil
Cleanup/optimization | Status: closed
Component: Documentation | Version: master
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Reply all
Reply to author
Forward
0 new messages