def filter(self, qs, value): if value: try: location = Location.objects.get(pk=value) sublocation_pks = Location.get_sites( location.get_descendants()).values_list('pk', flat=True) return qs.filter(pk__in=sublocation_pks) except Location.DoesNotExist: return qs.none() return qs
def filter(self, qs, value): if value: try: location = Location.objects.get(pk=value) descendant_sites = Location.get_sites( location.get_descendants()) return qs.filter(site__in=descendant_sites) except Location.DoesNotExist: return qs.none() return qs
def get_queryset(self): location = None try: location_pk = int(self.request.POST.get('location', 0)) except ValueError as e: if 'invalid literal for int()' in e.message: location_pk = 0 else: raise e if location_pk > 0: location = Location.objects.filter(pk=location_pk).first() if isinstance(location, Location): return location.get_site_descendants() else: # If there is no location with the requested PK (or no location was # given to filter the query results then just return the query # not filtered by location (i.e. the list of all sites). return Location.get_sites()