mirror of
https://github.com/ansible/awx.git
synced 2026-03-01 16:58:46 -03:30
Fix bug with parent_key filtering (#13957)
This was making host sub-list views non-functional specifically for constructed and smart inventory views would always return 0 results before this fix
This commit is contained in:
@@ -522,14 +522,16 @@ class SubListAPIView(ParentMixin, ListAPIView):
|
|||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
parent = self.get_parent_object()
|
parent = self.get_parent_object()
|
||||||
self.check_parent_access(parent)
|
self.check_parent_access(parent)
|
||||||
sublist_qs = self.get_sublist_queryset(parent)
|
|
||||||
if not self.filter_read_permission:
|
if not self.filter_read_permission:
|
||||||
return optimize_queryset(sublist_qs)
|
return optimize_queryset(self.get_sublist_queryset(parent))
|
||||||
qs = self.request.user.get_queryset(self.model).distinct()
|
qs = self.request.user.get_queryset(self.model)
|
||||||
return qs & sublist_qs
|
if hasattr(self, 'parent_key'):
|
||||||
|
# This is vastly preferable for ReverseForeignKey relationships
|
||||||
|
return qs.filter(**{self.parent_key: parent})
|
||||||
|
return qs.distinct() & self.get_sublist_queryset(parent).distinct()
|
||||||
|
|
||||||
def get_sublist_queryset(self, parent):
|
def get_sublist_queryset(self, parent):
|
||||||
return getattrd(parent, self.relationship).distinct()
|
return getattrd(parent, self.relationship)
|
||||||
|
|
||||||
|
|
||||||
class DestroyAPIView(generics.DestroyAPIView):
|
class DestroyAPIView(generics.DestroyAPIView):
|
||||||
@@ -578,15 +580,6 @@ class SubListCreateAPIView(SubListAPIView, ListCreateAPIView):
|
|||||||
d.update({'parent_key': getattr(self, 'parent_key', None)})
|
d.update({'parent_key': getattr(self, 'parent_key', None)})
|
||||||
return d
|
return d
|
||||||
|
|
||||||
def get_queryset(self):
|
|
||||||
if hasattr(self, 'parent_key'):
|
|
||||||
# Prefer this filtering because ForeignKey allows us more assumptions
|
|
||||||
parent = self.get_parent_object()
|
|
||||||
self.check_parent_access(parent)
|
|
||||||
qs = self.request.user.get_queryset(self.model)
|
|
||||||
return qs.filter(**{self.parent_key: parent})
|
|
||||||
return super(SubListCreateAPIView, self).get_queryset()
|
|
||||||
|
|
||||||
def create(self, request, *args, **kwargs):
|
def create(self, request, *args, **kwargs):
|
||||||
# If the object ID was not specified, it probably doesn't exist in the
|
# If the object ID was not specified, it probably doesn't exist in the
|
||||||
# DB yet. We want to see if we can create it. The URL may choose to
|
# DB yet. We want to see if we can create it. The URL may choose to
|
||||||
|
|||||||
Reference in New Issue
Block a user