Loading docs/ref/models/fields.txt +10 −0 Original line number Diff line number Diff line Loading @@ -97,6 +97,8 @@ second element is the human-readable name. For example:: Generally, it's best to define choices inside a model class, and to define a suitably-named constant for each value:: from django.db import models class Student(models.Model): FRESHMAN = 'FR' SOPHOMORE = 'SO' Loading Loading @@ -994,12 +996,15 @@ relationship with itself -- use ``models.ForeignKey('self')``. If you need to create a relationship on a model that has not yet been defined, you can use the name of the model, rather than the model object itself:: from django.db import models class Car(models.Model): manufacturer = models.ForeignKey('Manufacturer') # ... class Manufacturer(models.Model): # ... pass To refer to models defined in another application, you can explicitly specify a model with the full application label. For example, if the ``Manufacturer`` Loading Loading @@ -1132,6 +1137,9 @@ The possible values for :attr:`~ForeignKey.on_delete` are found in necessary to avoid executing queries at the time your models.py is imported:: from django.db import models from django.contrib.auth.models import User def get_sentinel_user(): return User.objects.get_or_create(username='deleted')[0] Loading Loading @@ -1204,6 +1212,8 @@ that control how the relationship functions. Only used in the definition of ManyToManyFields on self. Consider the following model:: from django.db import models class Person(models.Model): friends = models.ManyToManyField("self") Loading docs/ref/models/instances.txt +10 −0 Original line number Diff line number Diff line Loading @@ -34,6 +34,8 @@ that, you need to :meth:`~Model.save()`. 1. Add a classmethod on the model class:: from django.db import models class Book(models.Model): title = models.CharField(max_length=100) Loading Loading @@ -105,6 +107,7 @@ individually. You'll need to call ``full_clean`` manually when you want to run one-step model validation for your own manually created models. For example:: from django.core.exceptions import ValidationError try: article.full_clean() except ValidationError as e: Loading Loading @@ -132,6 +135,7 @@ automatically provide a value for a field, or to do validation that requires access to more than a single field:: def clean(self): import datetime from django.core.exceptions import ValidationError # Don't allow draft entries to have a pub_date. if self.status == 'draft' and self.pub_date is not None: Loading Loading @@ -434,6 +438,8 @@ representation of the model from the ``__unicode__()`` method. For example:: from django.db import models class Person(models.Model): first_name = models.CharField(max_length=50) last_name = models.CharField(max_length=50) Loading @@ -460,6 +466,9 @@ Thus, you should return a nice, human-readable string for the object's The previous :meth:`~Model.__unicode__()` example could be similarly written using ``__str__()`` like this:: from django.db import models from django.utils.encoding import force_bytes class Person(models.Model): first_name = models.CharField(max_length=50) last_name = models.CharField(max_length=50) Loading Loading @@ -490,6 +499,7 @@ function is usually the best approach.) For example:: def get_absolute_url(self): from django.core.urlresolvers import reverse return reverse('people.views.details', args=[str(self.id)]) One place Django uses ``get_absolute_url()`` is in the admin app. If an object Loading docs/ref/models/options.txt +6 −0 Original line number Diff line number Diff line Loading @@ -145,6 +145,12 @@ Django quotes column and table names behind the scenes. and a question has more than one answer, and the order of answers matters, you'd do this:: from django.db import models class Question(models.Model): text = models.TextField() # ... class Answer(models.Model): question = models.ForeignKey(Question) # ... Loading docs/ref/models/querysets.txt +7 −0 Original line number Diff line number Diff line Loading @@ -232,6 +232,7 @@ the model field that is being aggregated. For example, if you were manipulating a list of blogs, you may want to determine how many entries have been made in each blog:: >>> from django.db.models import Count >>> q = Blog.objects.annotate(Count('entry')) # The name of the first blog >>> q[0].name Loading Loading @@ -699,6 +700,8 @@ And here's ``select_related`` lookup:: ``select_related()`` follows foreign keys as far as possible. If you have the following models:: from django.db import models class City(models.Model): # ... pass Loading Loading @@ -814,6 +817,8 @@ that are supported by ``select_related``. It also supports prefetching of For example, suppose you have these models:: from django.db import models class Topping(models.Model): name = models.CharField(max_length=30) Loading Loading @@ -1565,6 +1570,7 @@ aggregated. For example, when you are working with blog entries, you may want to know the number of authors that have contributed blog entries:: >>> from django.db.models import Count >>> q = Blog.objects.aggregate(Count('entry')) {'entry__count': 16} Loading Loading @@ -2042,6 +2048,7 @@ Range test (inclusive). Example:: import datetime start_date = datetime.date(2005, 1, 1) end_date = datetime.date(2005, 3, 31) Entry.objects.filter(pub_date__range=(start_date, end_date)) Loading docs/ref/models/relations.txt +6 −2 Original line number Diff line number Diff line Loading @@ -12,8 +12,11 @@ Related objects reference * The "other side" of a :class:`~django.db.models.ForeignKey` relation. That is:: from django.db import models class Reporter(models.Model): ... # ... pass class Article(models.Model): reporter = models.ForeignKey(Reporter) Loading @@ -24,7 +27,8 @@ Related objects reference * Both sides of a :class:`~django.db.models.ManyToManyField` relation:: class Topping(models.Model): ... # ... pass class Pizza(models.Model): toppings = models.ManyToManyField(Topping) Loading Loading
docs/ref/models/fields.txt +10 −0 Original line number Diff line number Diff line Loading @@ -97,6 +97,8 @@ second element is the human-readable name. For example:: Generally, it's best to define choices inside a model class, and to define a suitably-named constant for each value:: from django.db import models class Student(models.Model): FRESHMAN = 'FR' SOPHOMORE = 'SO' Loading Loading @@ -994,12 +996,15 @@ relationship with itself -- use ``models.ForeignKey('self')``. If you need to create a relationship on a model that has not yet been defined, you can use the name of the model, rather than the model object itself:: from django.db import models class Car(models.Model): manufacturer = models.ForeignKey('Manufacturer') # ... class Manufacturer(models.Model): # ... pass To refer to models defined in another application, you can explicitly specify a model with the full application label. For example, if the ``Manufacturer`` Loading Loading @@ -1132,6 +1137,9 @@ The possible values for :attr:`~ForeignKey.on_delete` are found in necessary to avoid executing queries at the time your models.py is imported:: from django.db import models from django.contrib.auth.models import User def get_sentinel_user(): return User.objects.get_or_create(username='deleted')[0] Loading Loading @@ -1204,6 +1212,8 @@ that control how the relationship functions. Only used in the definition of ManyToManyFields on self. Consider the following model:: from django.db import models class Person(models.Model): friends = models.ManyToManyField("self") Loading
docs/ref/models/instances.txt +10 −0 Original line number Diff line number Diff line Loading @@ -34,6 +34,8 @@ that, you need to :meth:`~Model.save()`. 1. Add a classmethod on the model class:: from django.db import models class Book(models.Model): title = models.CharField(max_length=100) Loading Loading @@ -105,6 +107,7 @@ individually. You'll need to call ``full_clean`` manually when you want to run one-step model validation for your own manually created models. For example:: from django.core.exceptions import ValidationError try: article.full_clean() except ValidationError as e: Loading Loading @@ -132,6 +135,7 @@ automatically provide a value for a field, or to do validation that requires access to more than a single field:: def clean(self): import datetime from django.core.exceptions import ValidationError # Don't allow draft entries to have a pub_date. if self.status == 'draft' and self.pub_date is not None: Loading Loading @@ -434,6 +438,8 @@ representation of the model from the ``__unicode__()`` method. For example:: from django.db import models class Person(models.Model): first_name = models.CharField(max_length=50) last_name = models.CharField(max_length=50) Loading @@ -460,6 +466,9 @@ Thus, you should return a nice, human-readable string for the object's The previous :meth:`~Model.__unicode__()` example could be similarly written using ``__str__()`` like this:: from django.db import models from django.utils.encoding import force_bytes class Person(models.Model): first_name = models.CharField(max_length=50) last_name = models.CharField(max_length=50) Loading Loading @@ -490,6 +499,7 @@ function is usually the best approach.) For example:: def get_absolute_url(self): from django.core.urlresolvers import reverse return reverse('people.views.details', args=[str(self.id)]) One place Django uses ``get_absolute_url()`` is in the admin app. If an object Loading
docs/ref/models/options.txt +6 −0 Original line number Diff line number Diff line Loading @@ -145,6 +145,12 @@ Django quotes column and table names behind the scenes. and a question has more than one answer, and the order of answers matters, you'd do this:: from django.db import models class Question(models.Model): text = models.TextField() # ... class Answer(models.Model): question = models.ForeignKey(Question) # ... Loading
docs/ref/models/querysets.txt +7 −0 Original line number Diff line number Diff line Loading @@ -232,6 +232,7 @@ the model field that is being aggregated. For example, if you were manipulating a list of blogs, you may want to determine how many entries have been made in each blog:: >>> from django.db.models import Count >>> q = Blog.objects.annotate(Count('entry')) # The name of the first blog >>> q[0].name Loading Loading @@ -699,6 +700,8 @@ And here's ``select_related`` lookup:: ``select_related()`` follows foreign keys as far as possible. If you have the following models:: from django.db import models class City(models.Model): # ... pass Loading Loading @@ -814,6 +817,8 @@ that are supported by ``select_related``. It also supports prefetching of For example, suppose you have these models:: from django.db import models class Topping(models.Model): name = models.CharField(max_length=30) Loading Loading @@ -1565,6 +1570,7 @@ aggregated. For example, when you are working with blog entries, you may want to know the number of authors that have contributed blog entries:: >>> from django.db.models import Count >>> q = Blog.objects.aggregate(Count('entry')) {'entry__count': 16} Loading Loading @@ -2042,6 +2048,7 @@ Range test (inclusive). Example:: import datetime start_date = datetime.date(2005, 1, 1) end_date = datetime.date(2005, 3, 31) Entry.objects.filter(pub_date__range=(start_date, end_date)) Loading
docs/ref/models/relations.txt +6 −2 Original line number Diff line number Diff line Loading @@ -12,8 +12,11 @@ Related objects reference * The "other side" of a :class:`~django.db.models.ForeignKey` relation. That is:: from django.db import models class Reporter(models.Model): ... # ... pass class Article(models.Model): reporter = models.ForeignKey(Reporter) Loading @@ -24,7 +27,8 @@ Related objects reference * Both sides of a :class:`~django.db.models.ManyToManyField` relation:: class Topping(models.Model): ... # ... pass class Pizza(models.Model): toppings = models.ManyToManyField(Topping) Loading