10 examples of 'django order_by' in Python

Every line of 'django order_by' 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
793def order_by(self, *args):
794 """ Order the query by the given fields.
795
796 Parameters
797 ----------
798 args: List[str or column]
799 Fields to order by. A "-" prefix denotes decending.
800
801 Returns
802 -------
803 query: SQLQuerySet
804 A clone of this queryset with the ordering terms added.
805
806 """
807 order_clauses = self.order_clauses[:]
808 related_clauses = self.related_clauses[:]
809 model = self.proxy.model
810 for arg in args:
811 if isinstance(arg, str):
812 # Convert django-style to sqlalchemy ordering column
813 if arg[0] == '-':
814 field = arg[1:]
815 ascending = False
816 else:
817 field = arg
818 ascending = True
819
820 col = resolve_member_column(model, field, related_clauses)
821
822 if ascending:
823 clause = col.asc()
824 else:
825 clause = col.desc()
826 else:
827 clause = arg
828 if clause not in order_clauses:
829 order_clauses.append(clause)
830 return self.clone(order_clauses=order_clauses,
831 related_clauses=related_clauses)
810def test_order_by(self):
811 """Ensure that order_by() propagates to QuerySets and iteration."""
812 # Order by author and ensure it takes.
813 with self.assertNumQueries(0):
814 qss = self.all.order_by('title')
815
816 # Check the titles are properly ordered.
817 with self.assertNumQueries(2):
818 data = [it.title for it in qss]
819 self.assertEqual(data, sorted(self.TITLES_BY_PK))
15def _get_order_field_name(self):
16 return self.model.order_field_name
76def index_order(self, obj):
77 """Content for the `index_order` column"""
78 return mark_safe(
79 '<div>Drag</div>'
80 )
1065def order_by(self, *field_names):
1066 """
1067 Returns a new QuerySet instance with the ordering changed.
1068 We have a special field "_random"
1069 """
1070 obj = self._clone()
1071 obj._clear_ordering()
1072 self._insert_ordering(obj, *field_names)
1073 return obj
208def sortables_ordered(self, queryset):
209 """
210 Should return a queryset that orders the objects properly
211 (e.g. for nested categories the queryset should return:
212 Parent1, Paren1Child1, Parent1Child2, Parent2, Parent2Child1 etc)
213 """
214 return queryset
285def order_by(self, field):
286 """Order results returned according to a specified field. By default,
287 all sorting is case-sensitive and in ascending order.
288
289 :param field: the name (a string) of a field in the QuerySet's
290 :attr:`model`. If the field is prefixed with '-', results
291 will be sorted in descending order. If the field is
292 prefixed with '~', results will use a case-insensitive
293 sort. The flags '-' and '~' may be combined in any order.
294
295 Example usage::
296
297 queryset.filter(fulltext_terms='foo').order_by('-fulltext_score')
298 queryset.order_by('~name')
299
300 This method returns an updated copy of the QuerySet. It does not
301 modify the original.
302 """
303 sort_opts = {}
304 # use a regular expression to pull off any sort flags
305 match = self._sort_field_re.match(field)
306 field_parts = match.groupdict()
307 field = field_parts['field']
308 sort_flags = field_parts['flags']
309
310 # convert sort flags into options for xquery sort method
311 sort_opts = {}
312 if '-' in sort_flags:
313 sort_opts['ascending'] = False
314 if '~' in sort_flags:
315 sort_opts['case_insensitive'] = True
316
317 # TODO: allow multiple fields
318 xpath = _simple_fielddef_to_xpath(field, self.model) or field
319 qscopy = self._getCopy()
320 qscopy.query.sort(xpath, **sort_opts)
321 return qscopy
14def test_order_by_get(self):
15 url = reverse('user_filter_view') + '?order_by=last_login'
16 response = self.client.get(url)
17 self.assertEqual(response.status_code, 200)
21def get_paginate_by(self, queryset):
22 """Don't paginate if :py:attr:`mixup`."""
23 return self.paginate_by if not self.mixup else None
458def order(self, QuerySet, is_descending):
459 QuerySet = QuerySet.order_by(('-' if is_descending else '')
460 + 'sort_name', 'year_began')
461 return (QuerySet, True)

Related snippets