2005-07-29 17:15:40 +02:00
|
|
|
"""
|
|
|
|
6. Specifying ordering
|
|
|
|
|
|
|
|
Specify default ordering for a model using the ``ordering`` attribute, which
|
2008-02-15 12:38:53 +01:00
|
|
|
should be a list or tuple of field names. This tells Django how to order
|
2008-08-12 16:15:38 +02:00
|
|
|
``QuerySet`` results.
|
2005-07-29 17:15:40 +02:00
|
|
|
|
|
|
|
If a field name in ``ordering`` starts with a hyphen, that field will be
|
|
|
|
ordered in descending order. Otherwise, it'll be ordered in ascending order.
|
|
|
|
The special-case field name ``"?"`` specifies random order.
|
|
|
|
|
|
|
|
The ordering attribute is not required. If you leave it off, ordering will be
|
|
|
|
undefined -- not random, just undefined.
|
|
|
|
"""
|
|
|
|
|
2006-05-02 03:31:56 +02:00
|
|
|
from django.db import models
|
2012-08-12 12:32:08 +02:00
|
|
|
from django.utils.encoding import python_2_unicode_compatible
|
2005-07-29 17:15:40 +02:00
|
|
|
|
2010-10-11 20:17:37 +02:00
|
|
|
|
2012-08-12 12:32:08 +02:00
|
|
|
@python_2_unicode_compatible
|
2006-05-02 03:31:56 +02:00
|
|
|
class Article(models.Model):
|
2007-08-05 07:14:46 +02:00
|
|
|
headline = models.CharField(max_length=100)
|
2006-05-02 03:31:56 +02:00
|
|
|
pub_date = models.DateTimeField()
|
|
|
|
class Meta:
|
2005-08-26 00:51:30 +02:00
|
|
|
ordering = ('-pub_date', 'headline')
|
2005-07-29 17:15:40 +02:00
|
|
|
|
2012-08-12 12:32:08 +02:00
|
|
|
def __str__(self):
|
2005-07-29 17:15:40 +02:00
|
|
|
return self.headline
|
2012-02-04 20:56:40 +01:00
|
|
|
|
2012-08-12 12:32:08 +02:00
|
|
|
@python_2_unicode_compatible
|
2012-02-04 20:56:40 +01:00
|
|
|
class ArticlePKOrdering(models.Model):
|
|
|
|
headline = models.CharField(max_length=100)
|
|
|
|
pub_date = models.DateTimeField()
|
|
|
|
class Meta:
|
|
|
|
ordering = ('-pk',)
|
|
|
|
|
2012-08-12 12:32:08 +02:00
|
|
|
def __str__(self):
|
2012-02-04 20:56:40 +01:00
|
|
|
return self.headline
|