equal
deleted
inserted
replaced
|
1 """ |
|
2 6. Specifying ordering |
|
3 |
|
4 Specify default ordering for a model using the ``ordering`` attribute, which |
|
5 should be a list or tuple of field names. This tells Django how to order |
|
6 ``QuerySet`` results. |
|
7 |
|
8 If a field name in ``ordering`` starts with a hyphen, that field will be |
|
9 ordered in descending order. Otherwise, it'll be ordered in ascending order. |
|
10 The special-case field name ``"?"`` specifies random order. |
|
11 |
|
12 The ordering attribute is not required. If you leave it off, ordering will be |
|
13 undefined -- not random, just undefined. |
|
14 """ |
|
15 |
|
16 from django.db import models |
|
17 |
|
18 |
|
19 class Article(models.Model): |
|
20 headline = models.CharField(max_length=100) |
|
21 pub_date = models.DateTimeField() |
|
22 class Meta: |
|
23 ordering = ('-pub_date', 'headline') |
|
24 |
|
25 def __unicode__(self): |
|
26 return self.headline |