#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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/052.c41b6378762398c59f7381c8e3391603%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to