[Django] #30233: add get_queryset_with_parent_obj to InlineModelAdmin to support access parent_model's instance

3 views
Skip to first unread message

Django

unread,
Mar 3, 2019, 10:25:05 AM3/3/19
to django-...@googlegroups.com
#30233: add get_queryset_with_parent_obj to InlineModelAdmin to support access
parent_model's instance
-----------------------------------------+------------------------
Reporter: banxi1988 | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version: master
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 subclass admin.InlineModelAdmin.
I want to limit the queryset related to the parent_models's instance.

In my usecase, there's a Org Tree Model. and We can set managers for Org.
So I used the following ManyToManyField inline:


{{{
class OrgManagerRelationInline(admin.TabularInline):
model = OrgManagerRelation
extra = 1

def get_queryset(self, request):
qs = super(OrgManagerRelationInline, self).get_queryset(request)
return qs
}}}

But I want to limit to the manager candicates only belong to higher org
levels.
So I need to access the parent_model's instance. and there's also a SO
question
[https://stackoverflow.com/questions/32150088/django-access-the-parent-
instance-from-the-inline-model-admin]
But I dont'k want to use to unrobust hacking method.

So think we should provide a `get_queryset_with_parent_obj` or something
like this.

{{{
def get_queryset(self, request):
queryset = super().get_queryset(request)
if not self.has_view_or_change_permission(request):
queryset = queryset.none()
return queryset

def get_queryset_with_parent_obj(self,request,parent_obj):
return self.get_queryset(request)
}}}

and the calling code in `_create_formsets` can be changed to :


{{{
formset_params = {
'instance': obj,
'prefix': prefix,
'queryset':
inline.get_queryset_with_parent_obj(request,parent_obj=obj),
}
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/30233>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 6, 2019, 3:18:05 AM3/6/19
to django-...@googlegroups.com
#30233: add get_queryset_with_parent_obj to InlineModelAdmin to support access
parent_model's instance
-------------------------------+--------------------------------------
Reporter: banxi | Owner: nobody
Type: New feature | Status: closed
Component: contrib.admin | Version: master
Severity: Normal | Resolution: wontfix

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Carlton Gibson):

* status: new => closed
* resolution: => wontfix


Comment:

You can already solve this using
[https://docs.djangoproject.com/en/2.1/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_inline_instances
`ModelAdmin.get_inline_instances()`]. This is passed the ''parent
object'', which you could set as an attribute on the inline, to be used
later in `get_queryset()`.

--
Ticket URL: <https://code.djangoproject.com/ticket/30233#comment:1>

Reply all
Reply to author
Forward
0 new messages