10 examples of 'django queryset to list' in Python

Every line of 'django queryset to list' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
69def get_list_queryset(self):
70 obj = self.get_detail_object()
71 return getattr(obj, self.related_name).all()
33def test_queryset(self):
34 res = self.client.get('/list/authors/')
35 self.assertEqual(res.status_code, 200)
36 self.assertTemplateUsed(res, 'generic_views/author_list.html')
37 self.assertEqual(list(res.context['object_list']), list(Author.objects.all()))
38 self.assertIsInstance(res.context['view'], View)
39 self.assertIs(res.context['author_list'], res.context['object_list'])
40 self.assertIsNone(res.context['paginator'])
41 self.assertIsNone(res.context['page_obj'])
42 self.assertFalse(res.context['is_paginated'])
80def get_queryset(self, request):
81 qs = super(RelatedFieldAdmin, self).get_queryset(request)
82
83 # include all related fields in queryset
84 select_related = [field.rsplit('__', 1)[0] for field in self.list_display if is_field_allowed(field)]
85
86 # explicitly add contents of self.list_select_related to select_related
87 if self.list_select_related:
88 for field in self.list_select_related:
89 select_related.append(field)
90
91 # Include all foreign key fields in queryset.
92 # This is based on ChangeList.get_query_set().
93 # We have to duplicate it here because select_related() only works once.
94 # Can't just use list_select_related because we might have multiple__depth__fields it won't follow.
95 model = qs.model
96 for field_name in self.list_display:
97 try:
98 field = model._meta.get_field(field_name)
99 except models.FieldDoesNotExist:
100 continue
101 try:
102 remote_field = field.remote_field
103 except AttributeError: # for Django<1.9
104 remote_field = field.rel
105 if isinstance(remote_field, models.ManyToOneRel):
106 select_related.append(field_name)
107
108 return qs.select_related(*select_related)
41def get_queryset(self):
42 """Return queryset based on ModelAdmin.get_search_results()."""
43 qs = self.model_admin.get_queryset(self.request)
44 qs, search_use_distinct = self.model_admin.get_search_results(self.request, qs, self.term)
45 if search_use_distinct:
46 qs = qs.distinct()
47 return qs
276def get_values(self):
277 values = []
278 if isinstance(self.model_field, SmartListFilter):
279 values = [
280 SmartFilterValue(self.model_field.parameter_name, choice[1], choice[0], self.query_params)
281 for choice in self.model_field.lookups()
282 ]
283 elif self.model_field.choices:
284 values = [
285 SmartFilterValue(self.field_name, choice[1], choice[0], self.query_params)
286 for choice in self.model_field.choices
287 ]
288 elif type(self.model_field) == BooleanField:
289 values = [
290 SmartFilterValue(self.field_name, choice[1], choice[0], self.query_params)
291 for choice in ((1, _('Yes')), (0, _('No')))
292 ]
293 elif issubclass(type(self.model_field), ForeignKey):
294 pks = self.object_list.order_by().distinct().values_list('%s__pk' % self.field_name, flat=True)
295 remote_field = self.model_field.rel if hasattr(self.model_field, 'rel') else self.model_field.remote_field
296 qs = remote_field.model.objects.filter(pk__in=pks)
297 values = [SmartFilterValue(self.field_name, obj, str(obj.pk), self.query_params) for obj in qs]
298
299 return [SmartFilterValue(self.field_name, _("All"), None, self.query_params)] + values
40def get_queryset(self):
41 if self.queryset is not None:
42 return self.queryset._clone()
43 else:
44 return None
36def get_object_list(self):
37 raise NotImplementedError
54@property
55def queryset(self):
56 """
57 By default returns the self.model.objects
58 If you wish to restrict the records available
59 override this.
60 """
61 return self.model.objects
50def get_queryset(self):
51 """Get QuerySet from cached widget."""
52 kwargs = {
53 model_field_name: self.request.GET.get(form_field_name)
54 for form_field_name, model_field_name in self.widget.dependent_fields.items()
55 if form_field_name in self.request.GET and self.request.GET.get(form_field_name, '') != ''
56 }
57 return self.widget.filter_queryset(self.request, self.term, self.queryset, **kwargs)
106def get_queryset(self, **kwargs):
107 qs = super().get_queryset(**kwargs)
108 # allow a direct search of the child models
109 if self.request.GET.get('query'):
110 return qs
111 return qs.exclude(kv_tags__has_key='modelRef')

Related snippets