django ordering code example

Example 1: django order by

Entry.objects.filter(pub_date__year=2005).order_by('-pub_date', 'headline')

Example 2: django meta attributes

# All Django Class Meta Attributes
abstract
app_label
base_manager_name
db_table
Table names
db_tablespace
default_manager_name
default_related_name
get_latest_by
managed
order_with_respect_to
ordering
permissions
default_permissions
proxy
required_db_features
required_db_vendor
select_on_save
indexes
unique_together
index_together
constraints
verbose_name
verbose_name_plural

Example 3: django order by

class Meta:
        ordering = ('date',)

Example 4: django order by

#Add this to your models.py class, and dont remove the comma!
class Meta:
        ordering = ('yourfeild',)

Example 5: django db_table example

$python manage.py shell

>>> from myapp.models import Dreamreal, Online
>>> dr1 = Dreamreal()
>>> dr1.website = 'company1.com'
>>> dr1.name = 'company1'
>>> dr1.mail = 'contact@company1'
>>> dr1.phonenumber = '12345'
>>> dr1.save()
>>> dr2 = Dreamreal()
>>> dr1.website = 'company2.com'
>>> dr2.website = 'company2.com'
>>> dr2.name = 'company2'
>>> dr2.mail = 'contact@company2'
>>> dr2.phonenumber = '56789'
>>> dr2.save()