2009-04-01 02:14:12 +08:00
|
|
|
======
|
|
|
|
Models
|
|
|
|
======
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
.. module:: django.db.models
|
|
|
|
|
2013-10-11 05:52:50 +08:00
|
|
|
A model is the single, definitive source of information about your data. It
|
|
|
|
contains the essential fields and behaviors of the data you're storing.
|
|
|
|
Generally, each model maps to a single database table.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
The basics:
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* Each model is a Python class that subclasses
|
|
|
|
:class:`django.db.models.Model`.
|
2009-02-22 14:07:47 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* Each attribute of the model represents a database field.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* With all of this, Django gives you an automatically-generated
|
|
|
|
database-access API; see :doc:`/topics/db/queries`.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
Quick example
|
|
|
|
=============
|
|
|
|
|
|
|
|
This example model defines a ``Person``, which has a ``first_name`` and
|
|
|
|
``last_name``::
|
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
class Person(models.Model):
|
|
|
|
first_name = models.CharField(max_length=30)
|
|
|
|
last_name = models.CharField(max_length=30)
|
|
|
|
|
2008-08-26 03:29:10 +08:00
|
|
|
``first_name`` and ``last_name`` are fields_ of the model. Each field is
|
|
|
|
specified as a class attribute, and each attribute maps to a database column.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
The above ``Person`` model would create a database table like this:
|
|
|
|
|
|
|
|
.. code-block:: sql
|
|
|
|
|
|
|
|
CREATE TABLE myapp_person (
|
|
|
|
"id" serial NOT NULL PRIMARY KEY,
|
|
|
|
"first_name" varchar(30) NOT NULL,
|
|
|
|
"last_name" varchar(30) NOT NULL
|
|
|
|
);
|
|
|
|
|
|
|
|
Some technical notes:
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* The name of the table, ``myapp_person``, is automatically derived from
|
|
|
|
some model metadata but can be overridden. See :ref:`table-names` for more
|
2013-10-11 05:52:50 +08:00
|
|
|
details.
|
2009-02-22 14:07:47 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* An ``id`` field is added automatically, but this behavior can be
|
|
|
|
overridden. See :ref:`automatic-primary-key-fields`.
|
2009-02-22 14:07:47 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* The ``CREATE TABLE`` SQL in this example is formatted using PostgreSQL
|
|
|
|
syntax, but it's worth noting Django uses SQL tailored to the database
|
|
|
|
backend specified in your :doc:`settings file </topics/settings>`.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
Using models
|
|
|
|
============
|
|
|
|
|
|
|
|
Once you have defined your models, you need to tell Django you're going to *use*
|
|
|
|
those models. Do this by editing your settings file and changing the
|
|
|
|
:setting:`INSTALLED_APPS` setting to add the name of the module that contains
|
|
|
|
your ``models.py``.
|
|
|
|
|
|
|
|
For example, if the models for your application live in the module
|
2012-12-22 04:52:06 +08:00
|
|
|
``myapp.models`` (the package structure that is created for an
|
2008-08-24 06:25:40 +08:00
|
|
|
application by the :djadmin:`manage.py startapp <startapp>` script),
|
|
|
|
:setting:`INSTALLED_APPS` should read, in part::
|
|
|
|
|
2015-01-22 00:55:57 +08:00
|
|
|
INSTALLED_APPS = [
|
2008-08-24 06:25:40 +08:00
|
|
|
#...
|
2012-12-22 04:52:06 +08:00
|
|
|
'myapp',
|
2008-08-24 06:25:40 +08:00
|
|
|
#...
|
2015-01-22 00:55:57 +08:00
|
|
|
]
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
When you add new apps to :setting:`INSTALLED_APPS`, be sure to run
|
2013-07-25 23:19:36 +08:00
|
|
|
:djadmin:`manage.py migrate <migrate>`, optionally making migrations
|
|
|
|
for them first with :djadmin:`manage.py makemigrations <makemigrations>`.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
Fields
|
|
|
|
======
|
|
|
|
|
|
|
|
The most important part of a model -- and the only required part of a model --
|
|
|
|
is the list of database fields it defines. Fields are specified by class
|
2012-10-12 06:04:25 +08:00
|
|
|
attributes. Be careful not to choose field names that conflict with the
|
|
|
|
:doc:`models API </ref/models/instances>` like ``clean``, ``save``, or
|
|
|
|
``delete``.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
Example::
|
|
|
|
|
2013-05-18 18:12:26 +08:00
|
|
|
from django.db import models
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
class Musician(models.Model):
|
|
|
|
first_name = models.CharField(max_length=50)
|
|
|
|
last_name = models.CharField(max_length=50)
|
|
|
|
instrument = models.CharField(max_length=100)
|
|
|
|
|
|
|
|
class Album(models.Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
artist = models.ForeignKey(Musician, on_delete=models.CASCADE)
|
2008-08-24 06:25:40 +08:00
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
release_date = models.DateField()
|
|
|
|
num_stars = models.IntegerField()
|
|
|
|
|
|
|
|
Field types
|
|
|
|
-----------
|
|
|
|
|
|
|
|
Each field in your model should be an instance of the appropriate
|
|
|
|
:class:`~django.db.models.Field` class. Django uses the field class types to
|
|
|
|
determine a few things:
|
|
|
|
|
2016-02-12 03:27:15 +08:00
|
|
|
* The column type, which tells the database what kind of data to store (e.g.
|
|
|
|
``INTEGER``, ``VARCHAR``, ``TEXT``).
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2013-10-11 05:52:50 +08:00
|
|
|
* The default HTML :doc:`widget </ref/forms/widgets>` to use when rendering a form
|
2012-08-22 04:29:16 +08:00
|
|
|
field (e.g. ``<input type="text">``, ``<select>``).
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* The minimal validation requirements, used in Django's admin and in
|
|
|
|
automatically-generated forms.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
Django ships with dozens of built-in field types; you can find the complete list
|
|
|
|
in the :ref:`model field reference <model-field-types>`. You can easily write
|
|
|
|
your own fields if Django's built-in ones don't do the trick; see
|
2010-08-20 03:27:44 +08:00
|
|
|
:doc:`/howto/custom-model-fields`.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
Field options
|
|
|
|
-------------
|
|
|
|
|
|
|
|
Each field takes a certain set of field-specific arguments (documented in the
|
|
|
|
:ref:`model field reference <model-field-types>`). For example,
|
|
|
|
:class:`~django.db.models.CharField` (and its subclasses) require a
|
|
|
|
:attr:`~django.db.models.CharField.max_length` argument which specifies the size
|
|
|
|
of the ``VARCHAR`` database field used to store the data.
|
|
|
|
|
|
|
|
There's also a set of common arguments available to all field types. All are
|
|
|
|
optional. They're fully explained in the :ref:`reference
|
|
|
|
<common-model-field-options>`, but here's a quick summary of the most often-used
|
|
|
|
ones:
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
:attr:`~Field.null`
|
|
|
|
If ``True``, Django will store empty values as ``NULL`` in the database.
|
|
|
|
Default is ``False``.
|
|
|
|
|
|
|
|
:attr:`~Field.blank`
|
|
|
|
If ``True``, the field is allowed to be blank. Default is ``False``.
|
|
|
|
|
|
|
|
Note that this is different than :attr:`~Field.null`.
|
|
|
|
:attr:`~Field.null` is purely database-related, whereas
|
|
|
|
:attr:`~Field.blank` is validation-related. If a field has
|
2012-08-22 04:29:16 +08:00
|
|
|
:attr:`blank=True <Field.blank>`, form validation will
|
2011-10-14 08:12:01 +08:00
|
|
|
allow entry of an empty value. If a field has :attr:`blank=False
|
|
|
|
<Field.blank>`, the field will be required.
|
|
|
|
|
|
|
|
:attr:`~Field.choices`
|
|
|
|
An iterable (e.g., a list or tuple) of 2-tuples to use as choices for
|
2012-08-22 04:29:16 +08:00
|
|
|
this field. If this is given, the default form widget will be a select box
|
2011-10-14 08:12:01 +08:00
|
|
|
instead of the standard text field and will limit choices to the choices
|
|
|
|
given.
|
|
|
|
|
|
|
|
A choices list looks like this::
|
|
|
|
|
|
|
|
YEAR_IN_SCHOOL_CHOICES = (
|
2012-06-08 00:08:47 +08:00
|
|
|
('FR', 'Freshman'),
|
|
|
|
('SO', 'Sophomore'),
|
|
|
|
('JR', 'Junior'),
|
|
|
|
('SR', 'Senior'),
|
|
|
|
('GR', 'Graduate'),
|
2011-10-14 08:12:01 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
The first element in each tuple is the value that will be stored in the
|
2016-02-26 03:22:41 +08:00
|
|
|
database. The second element will be displayed by the default form widget
|
|
|
|
or in a :class:`~django.forms.ModelChoiceField`. Given a model instance,
|
|
|
|
the display value for a choices field can be accessed using the
|
|
|
|
``get_FOO_display()`` method. For example::
|
2011-10-14 08:12:01 +08:00
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
class Person(models.Model):
|
2012-06-06 17:54:26 +08:00
|
|
|
SHIRT_SIZES = (
|
2012-06-08 00:08:47 +08:00
|
|
|
('S', 'Small'),
|
|
|
|
('M', 'Medium'),
|
|
|
|
('L', 'Large'),
|
2008-08-24 06:25:40 +08:00
|
|
|
)
|
2011-10-14 08:12:01 +08:00
|
|
|
name = models.CharField(max_length=60)
|
2013-06-01 17:48:38 +08:00
|
|
|
shirt_size = models.CharField(max_length=1, choices=SHIRT_SIZES)
|
2009-05-02 15:40:25 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
::
|
|
|
|
|
2012-06-06 17:54:26 +08:00
|
|
|
>>> p = Person(name="Fred Flintstone", shirt_size="L")
|
2011-10-14 08:12:01 +08:00
|
|
|
>>> p.save()
|
2012-06-06 17:54:26 +08:00
|
|
|
>>> p.shirt_size
|
2014-03-23 04:30:49 +08:00
|
|
|
'L'
|
2012-06-06 17:54:26 +08:00
|
|
|
>>> p.get_shirt_size_display()
|
2014-03-23 04:30:49 +08:00
|
|
|
'Large'
|
2011-10-14 08:12:01 +08:00
|
|
|
|
|
|
|
:attr:`~Field.default`
|
|
|
|
The default value for the field. This can be a value or a callable
|
|
|
|
object. If callable it will be called every time a new object is
|
|
|
|
created.
|
|
|
|
|
|
|
|
:attr:`~Field.help_text`
|
2012-08-22 04:29:16 +08:00
|
|
|
Extra "help" text to be displayed with the form widget. It's useful for
|
|
|
|
documentation even if your field isn't used on a form.
|
2011-10-14 08:12:01 +08:00
|
|
|
|
|
|
|
:attr:`~Field.primary_key`
|
|
|
|
If ``True``, this field is the primary key for the model.
|
|
|
|
|
|
|
|
If you don't specify :attr:`primary_key=True <Field.primary_key>` for
|
|
|
|
any fields in your model, Django will automatically add an
|
|
|
|
:class:`IntegerField` to hold the primary key, so you don't need to set
|
|
|
|
:attr:`primary_key=True <Field.primary_key>` on any of your fields
|
|
|
|
unless you want to override the default primary-key behavior. For more,
|
|
|
|
see :ref:`automatic-primary-key-fields`.
|
|
|
|
|
2014-12-25 03:15:48 +08:00
|
|
|
The primary key field is read-only. If you change the value of the primary
|
|
|
|
key on an existing object and then save it, a new object will be created
|
|
|
|
alongside the old one. For example::
|
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
class Fruit(models.Model):
|
|
|
|
name = models.CharField(max_length=100, primary_key=True)
|
|
|
|
|
|
|
|
.. code-block:: pycon
|
|
|
|
|
|
|
|
>>> fruit = Fruit.objects.create(name='Apple')
|
|
|
|
>>> fruit.name = 'Pear'
|
|
|
|
>>> fruit.save()
|
|
|
|
>>> Fruit.objects.values_list('name', flat=True)
|
|
|
|
['Apple', 'Pear']
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
:attr:`~Field.unique`
|
|
|
|
If ``True``, this field must be unique throughout the table.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
Again, these are just short descriptions of the most common field options. Full
|
|
|
|
details can be found in the :ref:`common model field option reference
|
|
|
|
<common-model-field-options>`.
|
|
|
|
|
|
|
|
.. _automatic-primary-key-fields:
|
|
|
|
|
|
|
|
Automatic primary key fields
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
By default, Django gives each model the following field::
|
|
|
|
|
|
|
|
id = models.AutoField(primary_key=True)
|
|
|
|
|
|
|
|
This is an auto-incrementing primary key.
|
|
|
|
|
|
|
|
If you'd like to specify a custom primary key, just specify
|
|
|
|
:attr:`primary_key=True <Field.primary_key>` on one of your fields. If Django
|
|
|
|
sees you've explicitly set :attr:`Field.primary_key`, it won't add the automatic
|
|
|
|
``id`` column.
|
|
|
|
|
|
|
|
Each model requires exactly one field to have :attr:`primary_key=True
|
2013-10-11 05:52:50 +08:00
|
|
|
<Field.primary_key>` (either explicitly declared or automatically added).
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2009-02-22 14:10:29 +08:00
|
|
|
.. _verbose-field-names:
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
Verbose field names
|
|
|
|
-------------------
|
|
|
|
|
|
|
|
Each field type, except for :class:`~django.db.models.ForeignKey`,
|
|
|
|
:class:`~django.db.models.ManyToManyField` and
|
|
|
|
:class:`~django.db.models.OneToOneField`, takes an optional first positional
|
|
|
|
argument -- a verbose name. If the verbose name isn't given, Django will
|
|
|
|
automatically create it using the field's attribute name, converting underscores
|
|
|
|
to spaces.
|
|
|
|
|
2010-12-15 07:43:36 +08:00
|
|
|
In this example, the verbose name is ``"person's first name"``::
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2010-12-15 07:43:36 +08:00
|
|
|
first_name = models.CharField("person's first name", max_length=30)
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
In this example, the verbose name is ``"first name"``::
|
|
|
|
|
|
|
|
first_name = models.CharField(max_length=30)
|
|
|
|
|
|
|
|
:class:`~django.db.models.ForeignKey`,
|
|
|
|
:class:`~django.db.models.ManyToManyField` and
|
|
|
|
:class:`~django.db.models.OneToOneField` require the first argument to be a
|
|
|
|
model class, so use the :attr:`~Field.verbose_name` keyword argument::
|
|
|
|
|
2015-07-22 22:43:21 +08:00
|
|
|
poll = models.ForeignKey(
|
|
|
|
Poll,
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
verbose_name="the related poll",
|
|
|
|
)
|
2008-08-24 06:25:40 +08:00
|
|
|
sites = models.ManyToManyField(Site, verbose_name="list of sites")
|
2015-07-22 22:43:21 +08:00
|
|
|
place = models.OneToOneField(
|
|
|
|
Place,
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
verbose_name="related place",
|
|
|
|
)
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
The convention is not to capitalize the first letter of the
|
|
|
|
:attr:`~Field.verbose_name`. Django will automatically capitalize the first
|
|
|
|
letter where it needs to.
|
|
|
|
|
|
|
|
Relationships
|
|
|
|
-------------
|
|
|
|
|
|
|
|
Clearly, the power of relational databases lies in relating tables to each
|
|
|
|
other. Django offers ways to define the three most common types of database
|
2008-08-25 23:51:13 +08:00
|
|
|
relationships: many-to-one, many-to-many and one-to-one.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
Many-to-one relationships
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2011-03-05 06:29:16 +08:00
|
|
|
To define a many-to-one relationship, use :class:`django.db.models.ForeignKey`.
|
2008-08-24 06:25:40 +08:00
|
|
|
You use it just like any other :class:`~django.db.models.Field` type: by
|
|
|
|
including it as a class attribute of your model.
|
|
|
|
|
|
|
|
:class:`~django.db.models.ForeignKey` requires a positional argument: the class
|
|
|
|
to which the model is related.
|
|
|
|
|
|
|
|
For example, if a ``Car`` model has a ``Manufacturer`` -- that is, a
|
|
|
|
``Manufacturer`` makes multiple cars but each ``Car`` only has one
|
|
|
|
``Manufacturer`` -- use the following definitions::
|
|
|
|
|
2013-05-18 18:12:26 +08:00
|
|
|
from django.db import models
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
class Manufacturer(models.Model):
|
|
|
|
# ...
|
2013-05-18 18:12:26 +08:00
|
|
|
pass
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
class Car(models.Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
manufacturer = models.ForeignKey(Manufacturer, on_delete=models.CASCADE)
|
2008-08-24 06:25:40 +08:00
|
|
|
# ...
|
2009-02-22 14:07:47 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
You can also create :ref:`recursive relationships <recursive-relationships>` (an
|
2008-08-25 23:51:13 +08:00
|
|
|
object with a many-to-one relationship to itself) and :ref:`relationships to
|
2008-08-24 06:25:40 +08:00
|
|
|
models not yet defined <lazy-relationships>`; see :ref:`the model field
|
2008-08-25 23:51:13 +08:00
|
|
|
reference <ref-foreignkey>` for details.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
It's suggested, but not required, that the name of a
|
|
|
|
:class:`~django.db.models.ForeignKey` field (``manufacturer`` in the example
|
|
|
|
above) be the name of the model, lowercase. You can, of course, call the field
|
|
|
|
whatever you want. For example::
|
|
|
|
|
|
|
|
class Car(models.Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
company_that_makes_it = models.ForeignKey(
|
|
|
|
Manufacturer,
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
)
|
2008-08-24 06:25:40 +08:00
|
|
|
# ...
|
|
|
|
|
|
|
|
.. seealso::
|
|
|
|
|
2011-03-05 06:29:16 +08:00
|
|
|
:class:`~django.db.models.ForeignKey` fields accept a number of extra
|
|
|
|
arguments which are explained in :ref:`the model field reference
|
|
|
|
<foreign-key-arguments>`. These options help define how the relationship
|
|
|
|
should work; all are optional.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-03-05 06:29:16 +08:00
|
|
|
For details on accessing backwards-related objects, see the
|
2011-11-17 05:04:28 +08:00
|
|
|
:ref:`Following relationships backward example <backwards-related-objects>`.
|
2011-09-12 03:28:18 +08:00
|
|
|
|
2012-03-31 01:59:12 +08:00
|
|
|
For sample code, see the :doc:`Many-to-one relationship model example
|
|
|
|
</topics/db/examples/many_to_one>`.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
Many-to-many relationships
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
To define a many-to-many relationship, use
|
|
|
|
:class:`~django.db.models.ManyToManyField`. You use it just like any other
|
|
|
|
:class:`~django.db.models.Field` type: by including it as a class attribute of
|
|
|
|
your model.
|
|
|
|
|
|
|
|
:class:`~django.db.models.ManyToManyField` requires a positional argument: the
|
|
|
|
class to which the model is related.
|
|
|
|
|
|
|
|
For example, if a ``Pizza`` has multiple ``Topping`` objects -- that is, a
|
|
|
|
``Topping`` can be on multiple pizzas and each ``Pizza`` has multiple toppings
|
|
|
|
-- here's how you'd represent that::
|
|
|
|
|
2013-05-18 18:12:26 +08:00
|
|
|
from django.db import models
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
class Topping(models.Model):
|
|
|
|
# ...
|
2013-05-18 18:12:26 +08:00
|
|
|
pass
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
class Pizza(models.Model):
|
|
|
|
# ...
|
|
|
|
toppings = models.ManyToManyField(Topping)
|
|
|
|
|
|
|
|
As with :class:`~django.db.models.ForeignKey`, you can also create
|
|
|
|
:ref:`recursive relationships <recursive-relationships>` (an object with a
|
2010-08-07 10:43:35 +08:00
|
|
|
many-to-many relationship to itself) and :ref:`relationships to models not yet
|
2016-02-02 00:02:26 +08:00
|
|
|
defined <lazy-relationships>`.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
It's suggested, but not required, that the name of a
|
|
|
|
:class:`~django.db.models.ManyToManyField` (``toppings`` in the example above)
|
|
|
|
be a plural describing the set of related model objects.
|
|
|
|
|
2011-08-22 09:07:19 +08:00
|
|
|
It doesn't matter which model has the
|
|
|
|
:class:`~django.db.models.ManyToManyField`, but you should only put it in one
|
|
|
|
of the models -- not both.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2012-08-22 04:29:16 +08:00
|
|
|
Generally, :class:`~django.db.models.ManyToManyField` instances should go in
|
|
|
|
the object that's going to be edited on a form. In the above example,
|
|
|
|
``toppings`` is in ``Pizza`` (rather than ``Topping`` having a ``pizzas``
|
|
|
|
:class:`~django.db.models.ManyToManyField` ) because it's more natural to think
|
|
|
|
about a pizza having toppings than a topping being on multiple pizzas. The way
|
|
|
|
it's set up above, the ``Pizza`` form would let users select the toppings.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2009-02-22 14:07:47 +08:00
|
|
|
.. seealso::
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2012-03-31 01:59:12 +08:00
|
|
|
See the :doc:`Many-to-many relationship model example
|
|
|
|
</topics/db/examples/many_to_many>` for a full example.
|
|
|
|
|
|
|
|
:class:`~django.db.models.ManyToManyField` fields also accept a number of
|
|
|
|
extra arguments which are explained in :ref:`the model field reference
|
|
|
|
<manytomany-arguments>`. These options help define how the relationship
|
|
|
|
should work; all are optional.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2008-09-22 19:07:35 +08:00
|
|
|
.. _intermediary-manytomany:
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
Extra fields on many-to-many relationships
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
When you're only dealing with simple many-to-many relationships such as
|
2012-07-25 09:44:28 +08:00
|
|
|
mixing and matching pizzas and toppings, a standard
|
|
|
|
:class:`~django.db.models.ManyToManyField` is all you need. However, sometimes
|
2008-08-25 23:51:13 +08:00
|
|
|
you may need to associate data with the relationship between two models.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
For example, consider the case of an application tracking the musical groups
|
|
|
|
which musicians belong to. There is a many-to-many relationship between a person
|
2008-08-25 23:51:13 +08:00
|
|
|
and the groups of which they are a member, so you could use a
|
|
|
|
:class:`~django.db.models.ManyToManyField` to represent this relationship.
|
2009-02-22 14:07:47 +08:00
|
|
|
However, there is a lot of detail about the membership that you might want to
|
2008-08-25 23:51:13 +08:00
|
|
|
collect, such as the date at which the person joined the group.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
For these situations, Django allows you to specify the model that will be used
|
|
|
|
to govern the many-to-many relationship. You can then put extra fields on the
|
|
|
|
intermediate model. The intermediate model is associated with the
|
2009-02-22 14:07:47 +08:00
|
|
|
:class:`~django.db.models.ManyToManyField` using the
|
2009-03-09 11:33:54 +08:00
|
|
|
:attr:`through <ManyToManyField.through>` argument to point to the model
|
2008-08-24 06:25:40 +08:00
|
|
|
that will act as an intermediary. For our musician example, the code would look
|
|
|
|
something like this::
|
|
|
|
|
2013-05-18 18:12:26 +08:00
|
|
|
from django.db import models
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
class Person(models.Model):
|
|
|
|
name = models.CharField(max_length=128)
|
|
|
|
|
2017-01-19 00:51:29 +08:00
|
|
|
def __str__(self):
|
2008-08-24 06:25:40 +08:00
|
|
|
return self.name
|
|
|
|
|
|
|
|
class Group(models.Model):
|
|
|
|
name = models.CharField(max_length=128)
|
|
|
|
members = models.ManyToManyField(Person, through='Membership')
|
|
|
|
|
2017-01-19 00:51:29 +08:00
|
|
|
def __str__(self):
|
2008-08-24 06:25:40 +08:00
|
|
|
return self.name
|
|
|
|
|
|
|
|
class Membership(models.Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
person = models.ForeignKey(Person, on_delete=models.CASCADE)
|
|
|
|
group = models.ForeignKey(Group, on_delete=models.CASCADE)
|
2008-08-24 06:25:40 +08:00
|
|
|
date_joined = models.DateField()
|
|
|
|
invite_reason = models.CharField(max_length=64)
|
|
|
|
|
2009-02-22 14:07:47 +08:00
|
|
|
When you set up the intermediary model, you explicitly specify foreign
|
2014-02-20 02:01:55 +08:00
|
|
|
keys to the models that are involved in the many-to-many relationship. This
|
2008-08-24 06:25:40 +08:00
|
|
|
explicit declaration defines how the two models are related.
|
|
|
|
|
|
|
|
There are a few restrictions on the intermediate model:
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* Your intermediate model must contain one - and *only* one - foreign key
|
2014-03-07 19:56:28 +08:00
|
|
|
to the source model (this would be ``Group`` in our example), or you must
|
2014-02-20 02:01:55 +08:00
|
|
|
explicitly specify the foreign keys Django should use for the relationship
|
|
|
|
using :attr:`ManyToManyField.through_fields <ManyToManyField.through_fields>`.
|
|
|
|
If you have more than one foreign key and ``through_fields`` is not
|
|
|
|
specified, a validation error will be raised. A similar restriction applies
|
2014-03-07 19:56:28 +08:00
|
|
|
to the foreign key to the target model (this would be ``Person`` in our
|
2014-02-20 02:01:55 +08:00
|
|
|
example).
|
|
|
|
|
|
|
|
* For a model which has a many-to-many relationship to itself through an
|
|
|
|
intermediary model, two foreign keys to the same model are permitted, but
|
|
|
|
they will be treated as the two (different) sides of the many-to-many
|
|
|
|
relationship. If there are *more* than two foreign keys though, you
|
|
|
|
must also specify ``through_fields`` as above, or a validation error
|
|
|
|
will be raised.
|
2009-02-22 14:07:47 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* When defining a many-to-many relationship from a model to
|
|
|
|
itself, using an intermediary model, you *must* use
|
|
|
|
:attr:`symmetrical=False <ManyToManyField.symmetrical>` (see
|
|
|
|
:ref:`the model field reference <manytomany-arguments>`).
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2008-08-25 23:51:13 +08:00
|
|
|
Now that you have set up your :class:`~django.db.models.ManyToManyField` to use
|
2008-09-07 00:51:24 +08:00
|
|
|
your intermediary model (``Membership``, in this case), you're ready to start
|
2008-08-25 23:51:13 +08:00
|
|
|
creating some many-to-many relationships. You do this by creating instances of
|
|
|
|
the intermediate model::
|
2009-02-22 14:07:47 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
>>> ringo = Person.objects.create(name="Ringo Starr")
|
|
|
|
>>> paul = Person.objects.create(name="Paul McCartney")
|
|
|
|
>>> beatles = Group.objects.create(name="The Beatles")
|
|
|
|
>>> m1 = Membership(person=ringo, group=beatles,
|
2009-02-22 14:07:47 +08:00
|
|
|
... date_joined=date(1962, 8, 16),
|
2013-11-04 00:55:11 +08:00
|
|
|
... invite_reason="Needed a new drummer.")
|
2008-08-24 06:25:40 +08:00
|
|
|
>>> m1.save()
|
|
|
|
>>> beatles.members.all()
|
2015-10-06 07:07:34 +08:00
|
|
|
<QuerySet [<Person: Ringo Starr>]>
|
2008-08-24 06:25:40 +08:00
|
|
|
>>> ringo.group_set.all()
|
2015-10-06 07:07:34 +08:00
|
|
|
<QuerySet [<Group: The Beatles>]>
|
2008-08-24 06:25:40 +08:00
|
|
|
>>> m2 = Membership.objects.create(person=paul, group=beatles,
|
2009-02-22 14:07:47 +08:00
|
|
|
... date_joined=date(1960, 8, 1),
|
2013-11-04 00:55:11 +08:00
|
|
|
... invite_reason="Wanted to form a band.")
|
2008-08-24 06:25:40 +08:00
|
|
|
>>> beatles.members.all()
|
2015-10-06 07:07:34 +08:00
|
|
|
<QuerySet [<Person: Ringo Starr>, <Person: Paul McCartney>]>
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2015-10-09 05:17:10 +08:00
|
|
|
Unlike normal many-to-many fields, you *can't* use ``add()``, ``create()``,
|
|
|
|
or ``set()`` to create relationships::
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2017-01-10 06:00:44 +08:00
|
|
|
>>> # The following statements will not work
|
2008-08-24 06:25:40 +08:00
|
|
|
>>> beatles.members.add(john)
|
|
|
|
>>> beatles.members.create(name="George Harrison")
|
2015-10-09 05:17:10 +08:00
|
|
|
>>> beatles.members.set([john, paul, ringo, george])
|
2009-02-22 14:07:47 +08:00
|
|
|
|
2008-09-07 00:51:24 +08:00
|
|
|
Why? You can't just create a relationship between a ``Person`` and a ``Group``
|
|
|
|
- you need to specify all the detail for the relationship required by the
|
|
|
|
``Membership`` model. The simple ``add``, ``create`` and assignment calls
|
2008-08-24 06:25:40 +08:00
|
|
|
don't provide a way to specify this extra detail. As a result, they are
|
|
|
|
disabled for many-to-many relationships that use an intermediate model.
|
2008-09-07 00:51:24 +08:00
|
|
|
The only way to create this type of relationship is to create instances of the
|
|
|
|
intermediate model.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2010-12-06 07:52:23 +08:00
|
|
|
The :meth:`~django.db.models.fields.related.RelatedManager.remove` method is
|
2015-11-26 00:25:21 +08:00
|
|
|
disabled for similar reasons. For example, if the custom through table defined
|
|
|
|
by the intermediate model does not enforce uniqueness on the
|
|
|
|
``(model1, model2)`` pair, a ``remove()`` call would not provide enough
|
|
|
|
information as to which intermediate model instance should be deleted::
|
|
|
|
|
|
|
|
>>> Membership.objects.create(person=ringo, group=beatles,
|
|
|
|
... date_joined=date(1968, 9, 4),
|
|
|
|
... invite_reason="You've been gone for a month and we miss you.")
|
|
|
|
>>> beatles.members.all()
|
|
|
|
<QuerySet [<Person: Ringo Starr>, <Person: Paul McCartney>, <Person: Ringo Starr>]>
|
2017-01-10 06:00:44 +08:00
|
|
|
>>> # This will not work because it cannot tell which membership to remove
|
2015-11-26 00:25:21 +08:00
|
|
|
>>> beatles.members.remove(ringo)
|
|
|
|
|
|
|
|
However, the :meth:`~django.db.models.fields.related.RelatedManager.clear`
|
|
|
|
method can be used to remove all many-to-many relationships for an instance::
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2013-11-04 00:55:11 +08:00
|
|
|
>>> # Beatles have broken up
|
2008-08-24 06:25:40 +08:00
|
|
|
>>> beatles.members.clear()
|
2013-11-04 00:55:11 +08:00
|
|
|
>>> # Note that this deletes the intermediate model instances
|
|
|
|
>>> Membership.objects.all()
|
2015-10-06 07:07:34 +08:00
|
|
|
<QuerySet []>
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
Once you have established the many-to-many relationships by creating instances
|
2009-02-22 14:07:47 +08:00
|
|
|
of your intermediate model, you can issue queries. Just as with normal
|
|
|
|
many-to-many relationships, you can query using the attributes of the
|
2008-08-24 06:25:40 +08:00
|
|
|
many-to-many-related model::
|
|
|
|
|
|
|
|
# Find all the groups with a member whose name starts with 'Paul'
|
2009-05-18 00:20:11 +08:00
|
|
|
>>> Group.objects.filter(members__name__startswith='Paul')
|
2015-10-06 07:07:34 +08:00
|
|
|
<QuerySet [<Group: The Beatles>]>
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2008-09-07 00:51:24 +08:00
|
|
|
As you are using an intermediate model, you can also query on its attributes::
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
# Find all the members of the Beatles that joined after 1 Jan 1961
|
|
|
|
>>> Person.objects.filter(
|
|
|
|
... group__name='The Beatles',
|
|
|
|
... membership__date_joined__gt=date(1961,1,1))
|
2015-10-06 07:07:34 +08:00
|
|
|
<QuerySet [<Person: Ringo Starr]>
|
2009-02-22 14:07:47 +08:00
|
|
|
|
2011-09-12 03:28:18 +08:00
|
|
|
If you need to access a membership's information you may do so by directly
|
|
|
|
querying the ``Membership`` model::
|
|
|
|
|
|
|
|
>>> ringos_membership = Membership.objects.get(group=beatles, person=ringo)
|
|
|
|
>>> ringos_membership.date_joined
|
|
|
|
datetime.date(1962, 8, 16)
|
|
|
|
>>> ringos_membership.invite_reason
|
2014-03-23 04:30:49 +08:00
|
|
|
'Needed a new drummer.'
|
2011-09-12 03:28:18 +08:00
|
|
|
|
|
|
|
Another way to access the same information is by querying the
|
|
|
|
:ref:`many-to-many reverse relationship<m2m-reverse-relationships>` from a
|
|
|
|
``Person`` object::
|
|
|
|
|
|
|
|
>>> ringos_membership = ringo.membership_set.get(group=beatles)
|
|
|
|
>>> ringos_membership.date_joined
|
|
|
|
datetime.date(1962, 8, 16)
|
|
|
|
>>> ringos_membership.invite_reason
|
2014-03-23 04:30:49 +08:00
|
|
|
'Needed a new drummer.'
|
2011-09-12 03:28:18 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
One-to-one relationships
|
2008-09-01 17:32:41 +08:00
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2008-09-01 17:32:41 +08:00
|
|
|
To define a one-to-one relationship, use
|
|
|
|
:class:`~django.db.models.OneToOneField`. You use it just like any other
|
|
|
|
``Field`` type: by including it as a class attribute of your model.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2008-09-01 17:32:41 +08:00
|
|
|
This is most useful on the primary key of an object when that object "extends"
|
|
|
|
another object in some way.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2008-09-01 17:32:41 +08:00
|
|
|
:class:`~django.db.models.OneToOneField` requires a positional argument: the
|
|
|
|
class to which the model is related.
|
|
|
|
|
|
|
|
For example, if you were building a database of "places", you would
|
|
|
|
build pretty standard stuff such as address, phone number, etc. in the
|
|
|
|
database. Then, if you wanted to build a database of restaurants on
|
|
|
|
top of the places, instead of repeating yourself and replicating those
|
|
|
|
fields in the ``Restaurant`` model, you could make ``Restaurant`` have
|
|
|
|
a :class:`~django.db.models.OneToOneField` to ``Place`` (because a
|
|
|
|
restaurant "is a" place; in fact, to handle this you'd typically use
|
|
|
|
:ref:`inheritance <model-inheritance>`, which involves an implicit
|
|
|
|
one-to-one relation).
|
|
|
|
|
2016-02-02 00:02:26 +08:00
|
|
|
As with :class:`~django.db.models.ForeignKey`, a :ref:`recursive relationship
|
|
|
|
<recursive-relationships>` can be defined and :ref:`references to as-yet
|
|
|
|
undefined models <lazy-relationships>` can be made.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2008-09-01 17:32:41 +08:00
|
|
|
.. seealso::
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2012-03-31 01:59:12 +08:00
|
|
|
See the :doc:`One-to-one relationship model example
|
|
|
|
</topics/db/examples/one_to_one>` for a full example.
|
|
|
|
|
2016-02-02 00:02:26 +08:00
|
|
|
:class:`~django.db.models.OneToOneField` fields also accept an optional
|
|
|
|
:attr:`~django.db.models.OneToOneField.parent_link` argument.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2008-09-01 17:32:41 +08:00
|
|
|
:class:`~django.db.models.OneToOneField` classes used to automatically become
|
|
|
|
the primary key on a model. This is no longer true (although you can manually
|
|
|
|
pass in the :attr:`~django.db.models.Field.primary_key` argument if you like).
|
|
|
|
Thus, it's now possible to have multiple fields of type
|
|
|
|
:class:`~django.db.models.OneToOneField` on a single model.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
Models across files
|
2008-09-01 17:32:41 +08:00
|
|
|
-------------------
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2012-04-16 05:25:23 +08:00
|
|
|
It's perfectly OK to relate a model to one from another app. To do this, import
|
|
|
|
the related model at the top of the file where your model is defined. Then,
|
2008-08-24 06:25:40 +08:00
|
|
|
just refer to the other model class wherever needed. For example::
|
|
|
|
|
2013-05-18 18:12:26 +08:00
|
|
|
from django.db import models
|
2010-10-19 08:10:22 +08:00
|
|
|
from geography.models import ZipCode
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
class Restaurant(models.Model):
|
|
|
|
# ...
|
2015-07-22 22:43:21 +08:00
|
|
|
zip_code = models.ForeignKey(
|
|
|
|
ZipCode,
|
|
|
|
on_delete=models.SET_NULL,
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
)
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
Field name restrictions
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
Django places only two restrictions on model field names:
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
1. A field name cannot be a Python reserved word, because that would result
|
|
|
|
in a Python syntax error. For example::
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
class Example(models.Model):
|
|
|
|
pass = models.IntegerField() # 'pass' is a reserved word!
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
2. A field name cannot contain more than one underscore in a row, due to
|
|
|
|
the way Django's query lookup syntax works. For example::
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
class Example(models.Model):
|
|
|
|
foo__bar = models.IntegerField() # 'foo__bar' has two underscores!
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
These limitations can be worked around, though, because your field name doesn't
|
|
|
|
necessarily have to match your database column name. See the
|
|
|
|
:attr:`~Field.db_column` option.
|
|
|
|
|
|
|
|
SQL reserved words, such as ``join``, ``where`` or ``select``, *are* allowed as
|
|
|
|
model field names, because Django escapes all database table names and column
|
|
|
|
names in every underlying SQL query. It uses the quoting syntax of your
|
|
|
|
particular database engine.
|
|
|
|
|
|
|
|
Custom field types
|
|
|
|
------------------
|
|
|
|
|
|
|
|
If one of the existing model fields cannot be used to fit your purposes, or if
|
|
|
|
you wish to take advantage of some less common database column types, you can
|
|
|
|
create your own field class. Full coverage of creating your own fields is
|
2010-08-20 03:27:44 +08:00
|
|
|
provided in :doc:`/howto/custom-model-fields`.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
.. _meta-options:
|
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``Meta`` options
|
|
|
|
================
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
Give your model metadata by using an inner ``class Meta``, like so::
|
|
|
|
|
2013-05-18 18:12:26 +08:00
|
|
|
from django.db import models
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
class Ox(models.Model):
|
|
|
|
horn_length = models.IntegerField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ["horn_length"]
|
|
|
|
verbose_name_plural = "oxen"
|
|
|
|
|
|
|
|
Model metadata is "anything that's not a field", such as ordering options
|
|
|
|
(:attr:`~Options.ordering`), database table name (:attr:`~Options.db_table`), or
|
|
|
|
human-readable singular and plural names (:attr:`~Options.verbose_name` and
|
|
|
|
:attr:`~Options.verbose_name_plural`). None are required, and adding ``class
|
|
|
|
Meta`` to a model is completely optional.
|
|
|
|
|
2010-08-20 03:27:44 +08:00
|
|
|
A complete list of all possible ``Meta`` options can be found in the :doc:`model
|
|
|
|
option reference </ref/models/options>`.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2014-02-24 00:00:52 +08:00
|
|
|
.. _model-attributes:
|
|
|
|
|
|
|
|
Model attributes
|
|
|
|
================
|
|
|
|
|
2014-02-26 00:28:36 +08:00
|
|
|
``objects``
|
|
|
|
The most important attribute of a model is the
|
|
|
|
:class:`~django.db.models.Manager`. It's the interface through which
|
|
|
|
database query operations are provided to Django models and is used to
|
|
|
|
:ref:`retrieve the instances <retrieving-objects>` from the database. If no
|
2014-05-17 04:40:00 +08:00
|
|
|
custom ``Manager`` is defined, the default name is
|
|
|
|
:attr:`~django.db.models.Model.objects`. Managers are only accessible via
|
|
|
|
model classes, not the model instances.
|
2014-02-24 00:00:52 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. _model-methods:
|
|
|
|
|
|
|
|
Model methods
|
|
|
|
=============
|
|
|
|
|
|
|
|
Define custom methods on a model to add custom "row-level" functionality to your
|
|
|
|
objects. Whereas :class:`~django.db.models.Manager` methods are intended to do
|
|
|
|
"table-wide" things, model methods should act on a particular model instance.
|
|
|
|
|
|
|
|
This is a valuable technique for keeping business logic in one place -- the
|
|
|
|
model.
|
|
|
|
|
|
|
|
For example, this model has a few custom methods::
|
|
|
|
|
2013-05-18 18:12:26 +08:00
|
|
|
from django.db import models
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
class Person(models.Model):
|
|
|
|
first_name = models.CharField(max_length=50)
|
|
|
|
last_name = models.CharField(max_length=50)
|
|
|
|
birth_date = models.DateField()
|
|
|
|
|
|
|
|
def baby_boomer_status(self):
|
|
|
|
"Returns the person's baby-boomer status."
|
|
|
|
import datetime
|
|
|
|
if self.birth_date < datetime.date(1945, 8, 1):
|
|
|
|
return "Pre-boomer"
|
2012-11-23 05:29:39 +08:00
|
|
|
elif self.birth_date < datetime.date(1965, 1, 1):
|
|
|
|
return "Baby boomer"
|
|
|
|
else:
|
|
|
|
return "Post-boomer"
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2016-08-26 08:06:22 +08:00
|
|
|
@property
|
|
|
|
def full_name(self):
|
2008-08-24 06:25:40 +08:00
|
|
|
"Returns the person's full name."
|
|
|
|
return '%s %s' % (self.first_name, self.last_name)
|
|
|
|
|
2011-09-05 05:17:30 +08:00
|
|
|
The last method in this example is a :term:`property`.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2010-08-20 03:27:44 +08:00
|
|
|
The :doc:`model instance reference </ref/models/instances>` has a complete list
|
2008-08-25 23:51:13 +08:00
|
|
|
of :ref:`methods automatically given to each model <model-instance-methods>`.
|
|
|
|
You can override most of these -- see `overriding predefined model methods`_,
|
|
|
|
below -- but there are a couple that you'll almost always want to define:
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2017-01-19 00:51:29 +08:00
|
|
|
:meth:`~Model.__str__`
|
2017-01-21 05:04:05 +08:00
|
|
|
A Python "magic method" that returns a string representation of any
|
2011-10-14 08:12:01 +08:00
|
|
|
object. This is what Python and Django will use whenever a model
|
|
|
|
instance needs to be coerced and displayed as a plain string. Most
|
|
|
|
notably, this happens when you display an object in an interactive
|
|
|
|
console or in the admin.
|
2009-02-22 14:07:47 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
You'll always want to define this method; the default isn't very helpful
|
|
|
|
at all.
|
2009-02-22 14:07:47 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
:meth:`~Model.get_absolute_url`
|
|
|
|
This tells Django how to calculate the URL for an object. Django uses
|
|
|
|
this in its admin interface, and any time it needs to figure out a URL
|
|
|
|
for an object.
|
2009-02-22 14:07:47 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Any object that has a URL that uniquely identifies it should define this
|
|
|
|
method.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2010-01-08 08:17:33 +08:00
|
|
|
.. _overriding-model-methods:
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
Overriding predefined model methods
|
|
|
|
-----------------------------------
|
|
|
|
|
|
|
|
There's another set of :ref:`model methods <model-instance-methods>` that
|
|
|
|
encapsulate a bunch of database behavior that you'll want to customize. In
|
|
|
|
particular you'll often want to change the way :meth:`~Model.save` and
|
|
|
|
:meth:`~Model.delete` work.
|
|
|
|
|
|
|
|
You're free to override these methods (and any other model method) to alter
|
|
|
|
behavior.
|
|
|
|
|
|
|
|
A classic use-case for overriding the built-in methods is if you want something
|
2008-08-31 16:55:08 +08:00
|
|
|
to happen whenever you save an object. For example (see
|
|
|
|
:meth:`~Model.save` for documentation of the parameters it accepts)::
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2013-05-18 18:12:26 +08:00
|
|
|
from django.db import models
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
class Blog(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
tagline = models.TextField()
|
|
|
|
|
2010-01-08 08:17:33 +08:00
|
|
|
def save(self, *args, **kwargs):
|
2008-08-24 06:25:40 +08:00
|
|
|
do_something()
|
2017-01-22 14:57:14 +08:00
|
|
|
super().save(*args, **kwargs) # Call the "real" save() method.
|
2008-08-24 06:25:40 +08:00
|
|
|
do_something_else()
|
|
|
|
|
|
|
|
You can also prevent saving::
|
|
|
|
|
2013-05-18 18:12:26 +08:00
|
|
|
from django.db import models
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
class Blog(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
tagline = models.TextField()
|
|
|
|
|
2010-01-08 08:17:33 +08:00
|
|
|
def save(self, *args, **kwargs):
|
2008-08-24 06:25:40 +08:00
|
|
|
if self.name == "Yoko Ono's blog":
|
|
|
|
return # Yoko shall never have her own blog!
|
|
|
|
else:
|
2017-01-22 14:57:14 +08:00
|
|
|
super().save(*args, **kwargs) # Call the "real" save() method.
|
2010-01-08 08:17:33 +08:00
|
|
|
|
|
|
|
It's important to remember to call the superclass method -- that's
|
2017-01-22 14:57:14 +08:00
|
|
|
that ``super().save(*args, **kwargs)`` business -- to ensure
|
2010-01-08 08:17:33 +08:00
|
|
|
that the object still gets saved into the database. If you forget to
|
|
|
|
call the superclass method, the default behavior won't happen and the
|
|
|
|
database won't get touched.
|
|
|
|
|
|
|
|
It's also important that you pass through the arguments that can be
|
|
|
|
passed to the model method -- that's what the ``*args, **kwargs`` bit
|
|
|
|
does. Django will, from time to time, extend the capabilities of
|
|
|
|
built-in model methods, adding new arguments. If you use ``*args,
|
|
|
|
**kwargs`` in your method definitions, you are guaranteed that your
|
|
|
|
code will automatically support those arguments when they are added.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2012-10-04 20:03:48 +08:00
|
|
|
.. admonition:: Overridden model methods are not called on bulk operations
|
2010-12-28 07:46:06 +08:00
|
|
|
|
|
|
|
Note that the :meth:`~Model.delete()` method for an object is not
|
|
|
|
necessarily called when :ref:`deleting objects in bulk using a
|
2015-09-29 10:09:57 +08:00
|
|
|
QuerySet <topics-db-queries-delete>` or as a result of a :attr:`cascading
|
|
|
|
delete <django.db.models.ForeignKey.on_delete>`. To ensure customized
|
|
|
|
delete logic gets executed, you can use
|
|
|
|
:data:`~django.db.models.signals.pre_delete` and/or
|
|
|
|
:data:`~django.db.models.signals.post_delete` signals.
|
2010-12-28 07:46:06 +08:00
|
|
|
|
2012-10-04 20:03:48 +08:00
|
|
|
Unfortunately, there isn't a workaround when
|
|
|
|
:meth:`creating<django.db.models.query.QuerySet.bulk_create>` or
|
|
|
|
:meth:`updating<django.db.models.query.QuerySet.update>` objects in bulk,
|
|
|
|
since none of :meth:`~Model.save()`,
|
|
|
|
:data:`~django.db.models.signals.pre_save`, and
|
|
|
|
:data:`~django.db.models.signals.post_save` are called.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
Executing custom SQL
|
|
|
|
--------------------
|
|
|
|
|
|
|
|
Another common pattern is writing custom SQL statements in model methods and
|
2009-05-02 15:40:25 +08:00
|
|
|
module-level methods. For more details on using raw SQL, see the documentation
|
2010-08-20 03:27:44 +08:00
|
|
|
on :doc:`using raw SQL</topics/db/sql>`.
|
2008-08-31 18:13:32 +08:00
|
|
|
|
|
|
|
.. _model-inheritance:
|
|
|
|
|
|
|
|
Model inheritance
|
|
|
|
=================
|
|
|
|
|
|
|
|
Model inheritance in Django works almost identically to the way normal
|
2014-05-28 02:53:19 +08:00
|
|
|
class inheritance works in Python, but the basics at the beginning of the page
|
|
|
|
should still be followed. That means the base class should subclass
|
|
|
|
:class:`django.db.models.Model`.
|
|
|
|
|
|
|
|
The only decision you have to make is whether you want the parent models to be
|
|
|
|
models in their own right (with their own database tables), or if the parents
|
|
|
|
are just holders of common information that will only be visible through the
|
|
|
|
child models.
|
2008-08-31 18:13:32 +08:00
|
|
|
|
2009-03-18 17:47:08 +08:00
|
|
|
There are three styles of inheritance that are possible in Django.
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
1. Often, you will just want to use the parent class to hold information that
|
|
|
|
you don't want to have to type out for each child model. This class isn't
|
|
|
|
going to ever be used in isolation, so :ref:`abstract-base-classes` are
|
|
|
|
what you're after.
|
|
|
|
2. If you're subclassing an existing model (perhaps something from another
|
|
|
|
application entirely) and want each model to have its own database table,
|
|
|
|
:ref:`multi-table-inheritance` is the way to go.
|
|
|
|
3. Finally, if you only want to modify the Python-level behavior of a model,
|
|
|
|
without changing the models fields in any way, you can use
|
|
|
|
:ref:`proxy-models`.
|
2008-08-31 18:13:32 +08:00
|
|
|
|
|
|
|
.. _abstract-base-classes:
|
|
|
|
|
|
|
|
Abstract base classes
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
Abstract base classes are useful when you want to put some common
|
|
|
|
information into a number of other models. You write your base class
|
|
|
|
and put ``abstract=True`` in the :ref:`Meta <meta-options>`
|
|
|
|
class. This model will then not be used to create any database
|
|
|
|
table. Instead, when it is used as a base class for other models, its
|
|
|
|
fields will be added to those of the child class. It is an error to
|
|
|
|
have fields in the abstract base class with the same name as those in
|
|
|
|
the child (and Django will raise an exception).
|
|
|
|
|
|
|
|
An example::
|
|
|
|
|
2013-05-18 18:12:26 +08:00
|
|
|
from django.db import models
|
|
|
|
|
2008-08-31 18:13:32 +08:00
|
|
|
class CommonInfo(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
age = models.PositiveIntegerField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
class Student(CommonInfo):
|
|
|
|
home_group = models.CharField(max_length=5)
|
|
|
|
|
|
|
|
The ``Student`` model will have three fields: ``name``, ``age`` and
|
|
|
|
``home_group``. The ``CommonInfo`` model cannot be used as a normal Django
|
|
|
|
model, since it is an abstract base class. It does not generate a database
|
|
|
|
table or have a manager, and cannot be instantiated or saved directly.
|
|
|
|
|
|
|
|
For many uses, this type of model inheritance will be exactly what you want.
|
2016-01-01 03:29:52 +08:00
|
|
|
It provides a way to factor out common information at the Python level, while
|
2008-08-31 18:13:32 +08:00
|
|
|
still only creating one database table per child model at the database level.
|
|
|
|
|
|
|
|
``Meta`` inheritance
|
|
|
|
~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
When an abstract base class is created, Django makes any :ref:`Meta <meta-options>`
|
2008-09-07 00:51:24 +08:00
|
|
|
inner class you declared in the base class available as an
|
|
|
|
attribute. If a child class does not declare its own :ref:`Meta <meta-options>`
|
2008-08-31 18:13:32 +08:00
|
|
|
class, it will inherit the parent's :ref:`Meta <meta-options>`. If the child wants to
|
|
|
|
extend the parent's :ref:`Meta <meta-options>` class, it can subclass it. For example::
|
|
|
|
|
2013-05-18 18:12:26 +08:00
|
|
|
from django.db import models
|
|
|
|
|
2008-08-31 18:13:32 +08:00
|
|
|
class CommonInfo(models.Model):
|
2013-05-18 18:12:26 +08:00
|
|
|
# ...
|
2008-08-31 18:13:32 +08:00
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
ordering = ['name']
|
|
|
|
|
|
|
|
class Student(CommonInfo):
|
2013-05-18 18:12:26 +08:00
|
|
|
# ...
|
2008-08-31 18:13:32 +08:00
|
|
|
class Meta(CommonInfo.Meta):
|
|
|
|
db_table = 'student_info'
|
|
|
|
|
|
|
|
Django does make one adjustment to the :ref:`Meta <meta-options>` class of an abstract base
|
|
|
|
class: before installing the :ref:`Meta <meta-options>` attribute, it sets ``abstract=False``.
|
|
|
|
This means that children of abstract base classes don't automatically become
|
|
|
|
abstract classes themselves. Of course, you can make an abstract base class
|
|
|
|
that inherits from another abstract base class. You just need to remember to
|
|
|
|
explicitly set ``abstract=True`` each time.
|
|
|
|
|
|
|
|
Some attributes won't make sense to include in the :ref:`Meta <meta-options>` class of an
|
|
|
|
abstract base class. For example, including ``db_table`` would mean that all
|
|
|
|
the child classes (the ones that don't specify their own :ref:`Meta <meta-options>`) would use
|
|
|
|
the same database table, which is almost certainly not what you want.
|
|
|
|
|
|
|
|
.. _abstract-related-name:
|
|
|
|
|
2015-09-30 01:52:26 +08:00
|
|
|
Be careful with ``related_name`` and ``related_query_name``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
If you are using :attr:`~django.db.models.ForeignKey.related_name` or
|
|
|
|
:attr:`~django.db.models.ForeignKey.related_query_name` on a ``ForeignKey`` or
|
|
|
|
``ManyToManyField``, you must always specify a *unique* reverse name and query
|
|
|
|
name for the field. This would normally cause a problem in abstract base
|
|
|
|
classes, since the fields on this class are included into each of the child
|
|
|
|
classes, with exactly the same values for the attributes (including
|
|
|
|
:attr:`~django.db.models.ForeignKey.related_name` and
|
|
|
|
:attr:`~django.db.models.ForeignKey.related_query_name`) each time.
|
|
|
|
|
|
|
|
To work around this problem, when you are using
|
|
|
|
:attr:`~django.db.models.ForeignKey.related_name` or
|
|
|
|
:attr:`~django.db.models.ForeignKey.related_query_name` in an abstract base
|
|
|
|
class (only), part of the value should contain ``'%(app_label)s'`` and
|
|
|
|
``'%(class)s'``.
|
2010-01-10 04:03:52 +08:00
|
|
|
|
|
|
|
- ``'%(class)s'`` is replaced by the lower-cased name of the child class
|
|
|
|
that the field is used in.
|
|
|
|
- ``'%(app_label)s'`` is replaced by the lower-cased name of the app the child
|
|
|
|
class is contained within. Each installed application name must be unique
|
|
|
|
and the model class names within each app must also be unique, therefore the
|
2010-01-10 05:07:32 +08:00
|
|
|
resulting name will end up being different.
|
|
|
|
|
|
|
|
For example, given an app ``common/models.py``::
|
2008-08-31 18:13:32 +08:00
|
|
|
|
2013-05-18 18:12:26 +08:00
|
|
|
from django.db import models
|
|
|
|
|
2008-08-31 18:13:32 +08:00
|
|
|
class Base(models.Model):
|
2015-09-30 01:52:26 +08:00
|
|
|
m2m = models.ManyToManyField(
|
|
|
|
OtherModel,
|
|
|
|
related_name="%(app_label)s_%(class)s_related",
|
|
|
|
related_query_name="%(app_label)s_%(class)ss",
|
|
|
|
)
|
2008-08-31 18:13:32 +08:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
class ChildA(Base):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class ChildB(Base):
|
|
|
|
pass
|
|
|
|
|
2010-01-10 04:03:52 +08:00
|
|
|
Along with another app ``rare/models.py``::
|
2010-01-10 05:07:32 +08:00
|
|
|
|
2010-01-10 04:03:52 +08:00
|
|
|
from common.models import Base
|
|
|
|
|
|
|
|
class ChildB(Base):
|
|
|
|
pass
|
|
|
|
|
2011-11-10 16:41:21 +08:00
|
|
|
The reverse name of the ``common.ChildA.m2m`` field will be
|
2015-09-30 01:52:26 +08:00
|
|
|
``common_childa_related`` and the reverse query name will be ``common_childas``.
|
|
|
|
The reverse name of the ``common.ChildB.m2m`` field will be
|
|
|
|
``common_childb_related`` and the reverse query name will be
|
|
|
|
``common_childbs``. Finally, the reverse name of the ``rare.ChildB.m2m`` field
|
|
|
|
will be ``rare_childb_related`` and the reverse query name will be
|
2016-11-16 20:28:20 +08:00
|
|
|
``rare_childbs``. It's up to you how you use the ``'%(class)s'`` and
|
|
|
|
``'%(app_label)s'`` portion to construct your related name or related query name
|
2015-09-30 01:52:26 +08:00
|
|
|
but if you forget to use it, Django will raise errors when you perform system
|
|
|
|
checks (or run :djadmin:`migrate`).
|
2008-08-31 18:13:32 +08:00
|
|
|
|
2010-01-10 04:03:52 +08:00
|
|
|
If you don't specify a :attr:`~django.db.models.ForeignKey.related_name`
|
|
|
|
attribute for a field in an abstract base class, the default reverse name will
|
|
|
|
be the name of the child class followed by ``'_set'``, just as it normally
|
|
|
|
would be if you'd declared the field directly on the child class. For example,
|
|
|
|
in the above code, if the :attr:`~django.db.models.ForeignKey.related_name`
|
|
|
|
attribute was omitted, the reverse name for the ``m2m`` field would be
|
|
|
|
``childa_set`` in the ``ChildA`` case and ``childb_set`` for the ``ChildB``
|
|
|
|
field.
|
2008-08-31 18:13:32 +08:00
|
|
|
|
|
|
|
.. _multi-table-inheritance:
|
|
|
|
|
|
|
|
Multi-table inheritance
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
The second type of model inheritance supported by Django is when each model in
|
|
|
|
the hierarchy is a model all by itself. Each model corresponds to its own
|
2008-09-07 00:51:24 +08:00
|
|
|
database table and can be queried and created individually. The inheritance
|
2008-08-31 18:13:32 +08:00
|
|
|
relationship introduces links between the child model and each of its parents
|
2011-03-05 06:29:16 +08:00
|
|
|
(via an automatically-created :class:`~django.db.models.OneToOneField`).
|
2008-08-31 18:13:32 +08:00
|
|
|
For example::
|
|
|
|
|
2013-05-18 18:12:26 +08:00
|
|
|
from django.db import models
|
|
|
|
|
2008-08-31 18:13:32 +08:00
|
|
|
class Place(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
address = models.CharField(max_length=80)
|
|
|
|
|
|
|
|
class Restaurant(Place):
|
2014-08-14 00:33:06 +08:00
|
|
|
serves_hot_dogs = models.BooleanField(default=False)
|
|
|
|
serves_pizza = models.BooleanField(default=False)
|
2008-08-31 18:13:32 +08:00
|
|
|
|
|
|
|
All of the fields of ``Place`` will also be available in ``Restaurant``,
|
|
|
|
although the data will reside in a different database table. So these are both
|
|
|
|
possible::
|
|
|
|
|
|
|
|
>>> Place.objects.filter(name="Bob's Cafe")
|
|
|
|
>>> Restaurant.objects.filter(name="Bob's Cafe")
|
|
|
|
|
|
|
|
If you have a ``Place`` that is also a ``Restaurant``, you can get from the
|
|
|
|
``Place`` object to the ``Restaurant`` object by using the lower-case version
|
|
|
|
of the model name::
|
|
|
|
|
2010-01-11 02:50:28 +08:00
|
|
|
>>> p = Place.objects.get(id=12)
|
|
|
|
# If p is a Restaurant object, this will give the child class:
|
2008-08-31 18:13:32 +08:00
|
|
|
>>> p.restaurant
|
|
|
|
<Restaurant: ...>
|
|
|
|
|
|
|
|
However, if ``p`` in the above example was *not* a ``Restaurant`` (it had been
|
|
|
|
created directly as a ``Place`` object or was the parent of some other class),
|
2014-02-23 01:30:28 +08:00
|
|
|
referring to ``p.restaurant`` would raise a ``Restaurant.DoesNotExist``
|
|
|
|
exception.
|
2008-08-31 18:13:32 +08:00
|
|
|
|
2017-04-02 09:21:15 +08:00
|
|
|
The automatically-created :class:`~django.db.models.OneToOneField` on
|
|
|
|
``Restaurant`` that links it to ``Place`` looks like this::
|
|
|
|
|
|
|
|
place_ptr = models.OneToOneField(
|
|
|
|
Place, on_delete=models.CASCADE,
|
|
|
|
parent_link=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
You can override that field by declaring your own
|
|
|
|
:class:`~django.db.models.OneToOneField` with :attr:`parent_link=True
|
|
|
|
<django.db.models.OneToOneField.parent_link>` on ``Restaurant``.
|
|
|
|
|
2016-09-29 00:21:03 +08:00
|
|
|
.. _meta-and-multi-table-inheritance:
|
|
|
|
|
2008-08-31 18:13:32 +08:00
|
|
|
``Meta`` and multi-table inheritance
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
In the multi-table inheritance situation, it doesn't make sense for a child
|
|
|
|
class to inherit from its parent's :ref:`Meta <meta-options>` class. All the :ref:`Meta <meta-options>` options
|
|
|
|
have already been applied to the parent class and applying them again would
|
2009-03-18 17:47:08 +08:00
|
|
|
normally only lead to contradictory behavior (this is in contrast with the
|
2008-08-31 18:13:32 +08:00
|
|
|
abstract base class case, where the base class doesn't exist in its own
|
|
|
|
right).
|
|
|
|
|
2009-03-18 17:47:08 +08:00
|
|
|
So a child model does not have access to its parent's :ref:`Meta
|
|
|
|
<meta-options>` class. However, there are a few limited cases where the child
|
|
|
|
inherits behavior from the parent: if the child does not specify an
|
2010-12-06 07:52:23 +08:00
|
|
|
:attr:`~django.db.models.Options.ordering` attribute or a
|
|
|
|
:attr:`~django.db.models.Options.get_latest_by` attribute, it will inherit
|
2009-03-18 17:47:08 +08:00
|
|
|
these from its parent.
|
2008-08-31 18:13:32 +08:00
|
|
|
|
|
|
|
If the parent has an ordering and you don't want the child to have any natural
|
2008-09-07 00:51:24 +08:00
|
|
|
ordering, you can explicitly disable it::
|
2008-08-31 18:13:32 +08:00
|
|
|
|
|
|
|
class ChildModel(ParentModel):
|
2013-05-18 18:12:26 +08:00
|
|
|
# ...
|
2008-08-31 18:13:32 +08:00
|
|
|
class Meta:
|
|
|
|
# Remove parent's ordering effect
|
|
|
|
ordering = []
|
|
|
|
|
|
|
|
Inheritance and reverse relations
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Because multi-table inheritance uses an implicit
|
2010-12-06 07:52:23 +08:00
|
|
|
:class:`~django.db.models.OneToOneField` to link the child and
|
2008-08-31 18:13:32 +08:00
|
|
|
the parent, it's possible to move from the parent down to the child,
|
|
|
|
as in the above example. However, this uses up the name that is the
|
|
|
|
default :attr:`~django.db.models.ForeignKey.related_name` value for
|
2010-12-06 07:52:23 +08:00
|
|
|
:class:`~django.db.models.ForeignKey` and
|
|
|
|
:class:`~django.db.models.ManyToManyField` relations. If you
|
2014-08-19 18:27:27 +08:00
|
|
|
are putting those types of relations on a subclass of the parent model, you
|
|
|
|
**must** specify the :attr:`~django.db.models.ForeignKey.related_name`
|
|
|
|
attribute on each such field. If you forget, Django will raise a validation
|
|
|
|
error.
|
2008-08-31 18:13:32 +08:00
|
|
|
|
|
|
|
For example, using the above ``Place`` class again, let's create another
|
2010-12-06 07:52:23 +08:00
|
|
|
subclass with a :class:`~django.db.models.ManyToManyField`::
|
2008-08-31 18:13:32 +08:00
|
|
|
|
|
|
|
class Supplier(Place):
|
2014-08-19 18:27:27 +08:00
|
|
|
customers = models.ManyToManyField(Place)
|
2008-08-31 18:13:32 +08:00
|
|
|
|
2014-08-19 18:27:27 +08:00
|
|
|
This results in the error::
|
|
|
|
|
|
|
|
Reverse query name for 'Supplier.customers' clashes with reverse query
|
|
|
|
name for 'Supplier.place_ptr'.
|
|
|
|
|
|
|
|
HINT: Add or change a related_name argument to the definition for
|
|
|
|
'Supplier.customers' or 'Supplier.place_ptr'.
|
|
|
|
|
|
|
|
Adding ``related_name`` to the ``customers`` field as follows would resolve the
|
|
|
|
error: ``models.ManyToManyField(Place, related_name='provider')``.
|
2008-08-31 18:13:32 +08:00
|
|
|
|
|
|
|
Specifying the parent link field
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
As mentioned, Django will automatically create a
|
2010-12-06 07:52:23 +08:00
|
|
|
:class:`~django.db.models.OneToOneField` linking your child
|
2015-10-31 19:38:58 +08:00
|
|
|
class back to any non-abstract parent models. If you want to control the
|
2008-08-31 18:13:32 +08:00
|
|
|
name of the attribute linking back to the parent, you can create your
|
2010-12-06 07:52:23 +08:00
|
|
|
own :class:`~django.db.models.OneToOneField` and set
|
|
|
|
:attr:`parent_link=True <django.db.models.OneToOneField.parent_link>`
|
2008-08-31 18:13:32 +08:00
|
|
|
to indicate that your field is the link back to the parent class.
|
|
|
|
|
2009-03-18 17:47:08 +08:00
|
|
|
.. _proxy-models:
|
|
|
|
|
|
|
|
Proxy models
|
|
|
|
------------
|
|
|
|
|
|
|
|
When using :ref:`multi-table inheritance <multi-table-inheritance>`, a new
|
|
|
|
database table is created for each subclass of a model. This is usually the
|
|
|
|
desired behavior, since the subclass needs a place to store any additional
|
|
|
|
data fields that are not present on the base class. Sometimes, however, you
|
|
|
|
only want to change the Python behavior of a model -- perhaps to change the
|
|
|
|
default manager, or add a new method.
|
|
|
|
|
|
|
|
This is what proxy model inheritance is for: creating a *proxy* for the
|
|
|
|
original model. You can create, delete and update instances of the proxy model
|
|
|
|
and all the data will be saved as if you were using the original (non-proxied)
|
|
|
|
model. The difference is that you can change things like the default model
|
|
|
|
ordering or the default manager in the proxy, without having to alter the
|
|
|
|
original.
|
|
|
|
|
|
|
|
Proxy models are declared like normal models. You tell Django that it's a
|
Fixed #10389, #10501, #10502, #10540, #10562, #10563, #10564, #10565, #10568, #10569, #10614, #10617, #10619 -- Fixed several typos as well as a couple minor issues in the docs, patches from timo, nih, bthomas, rduffield, UloPe, and sebleier@gmail.com.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10242 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-03-31 15:01:01 +08:00
|
|
|
proxy model by setting the :attr:`~django.db.models.Options.proxy` attribute of
|
|
|
|
the ``Meta`` class to ``True``.
|
2009-03-18 17:47:08 +08:00
|
|
|
|
2013-05-18 18:12:26 +08:00
|
|
|
For example, suppose you want to add a method to the ``Person`` model. You can do it like this::
|
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
class Person(models.Model):
|
|
|
|
first_name = models.CharField(max_length=30)
|
|
|
|
last_name = models.CharField(max_length=30)
|
2009-03-18 17:47:08 +08:00
|
|
|
|
2012-12-29 03:00:11 +08:00
|
|
|
class MyPerson(Person):
|
2009-03-18 17:47:08 +08:00
|
|
|
class Meta:
|
|
|
|
proxy = True
|
|
|
|
|
|
|
|
def do_something(self):
|
2013-05-18 18:12:26 +08:00
|
|
|
# ...
|
|
|
|
pass
|
2009-03-18 17:47:08 +08:00
|
|
|
|
2012-12-29 03:00:11 +08:00
|
|
|
The ``MyPerson`` class operates on the same database table as its parent
|
|
|
|
``Person`` class. In particular, any new instances of ``Person`` will also be
|
|
|
|
accessible through ``MyPerson``, and vice-versa::
|
2009-03-18 17:47:08 +08:00
|
|
|
|
2012-12-29 03:00:11 +08:00
|
|
|
>>> p = Person.objects.create(first_name="foobar")
|
|
|
|
>>> MyPerson.objects.get(first_name="foobar")
|
|
|
|
<MyPerson: foobar>
|
2009-03-18 17:47:08 +08:00
|
|
|
|
2012-12-29 03:00:11 +08:00
|
|
|
You could also use a proxy model to define a different default ordering on
|
|
|
|
a model. You might not always want to order the ``Person`` model, but regularly
|
|
|
|
order by the ``last_name`` attribute when you use the proxy. This is easy::
|
2009-03-18 17:47:08 +08:00
|
|
|
|
2012-12-29 03:00:11 +08:00
|
|
|
class OrderedPerson(Person):
|
2009-03-18 17:47:08 +08:00
|
|
|
class Meta:
|
2012-12-29 03:00:11 +08:00
|
|
|
ordering = ["last_name"]
|
2009-03-18 17:47:08 +08:00
|
|
|
proxy = True
|
|
|
|
|
2012-12-29 03:00:11 +08:00
|
|
|
Now normal ``Person`` queries will be unordered
|
|
|
|
and ``OrderedPerson`` queries will be ordered by ``last_name``.
|
2009-03-18 17:47:08 +08:00
|
|
|
|
2016-09-29 00:21:03 +08:00
|
|
|
Proxy models inherit ``Meta`` attributes :ref:`in the same way as regular
|
|
|
|
models <meta-and-multi-table-inheritance>`.
|
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``QuerySet``\s still return the model that was requested
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2009-03-18 17:47:08 +08:00
|
|
|
|
2012-12-29 03:00:11 +08:00
|
|
|
There is no way to have Django return, say, a ``MyPerson`` object whenever you
|
|
|
|
query for ``Person`` objects. A queryset for ``Person`` objects will return
|
|
|
|
those types of objects. The whole point of proxy objects is that code relying
|
|
|
|
on the original ``Person`` will use those and your own code can use the
|
|
|
|
extensions you included (that no other code is relying on anyway). It is not
|
|
|
|
a way to replace the ``Person`` (or any other) model everywhere with something
|
|
|
|
of your own creation.
|
2009-03-18 17:47:08 +08:00
|
|
|
|
|
|
|
Base class restrictions
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
A proxy model must inherit from exactly one non-abstract model class. You
|
|
|
|
can't inherit from multiple non-abstract models as the proxy model doesn't
|
|
|
|
provide any connection between the rows in the different database tables. A
|
|
|
|
proxy model can inherit from any number of abstract model classes, providing
|
2016-03-28 03:20:54 +08:00
|
|
|
they do *not* define any model fields. A proxy model may also inherit from any
|
|
|
|
number of proxy models that share a common non-abstract parent class.
|
|
|
|
|
2009-03-18 17:47:08 +08:00
|
|
|
Proxy model managers
|
|
|
|
~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
If you don't specify any model managers on a proxy model, it inherits the
|
|
|
|
managers from its model parents. If you define a manager on the proxy model,
|
|
|
|
it will become the default, although any managers defined on the parent
|
|
|
|
classes will still be available.
|
|
|
|
|
|
|
|
Continuing our example from above, you could change the default manager used
|
2012-12-29 03:00:11 +08:00
|
|
|
when you query the ``Person`` model like this::
|
2009-03-18 17:47:08 +08:00
|
|
|
|
2013-05-18 18:12:26 +08:00
|
|
|
from django.db import models
|
|
|
|
|
2009-03-18 17:47:08 +08:00
|
|
|
class NewManager(models.Manager):
|
2013-05-18 18:12:26 +08:00
|
|
|
# ...
|
|
|
|
pass
|
2009-03-18 17:47:08 +08:00
|
|
|
|
2012-12-29 03:00:11 +08:00
|
|
|
class MyPerson(Person):
|
2009-03-18 17:47:08 +08:00
|
|
|
objects = NewManager()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
proxy = True
|
|
|
|
|
|
|
|
If you wanted to add a new manager to the Proxy, without replacing the
|
|
|
|
existing default, you can use the techniques described in the :ref:`custom
|
|
|
|
manager <custom-managers-and-inheritance>` documentation: create a base class
|
|
|
|
containing the new managers and inherit that after the primary base class::
|
|
|
|
|
|
|
|
# Create an abstract class for the new manager.
|
2009-05-18 23:36:18 +08:00
|
|
|
class ExtraManagers(models.Model):
|
2009-03-18 17:47:08 +08:00
|
|
|
secondary = NewManager()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
2012-12-29 03:00:11 +08:00
|
|
|
class MyPerson(Person, ExtraManagers):
|
2009-03-18 17:47:08 +08:00
|
|
|
class Meta:
|
|
|
|
proxy = True
|
|
|
|
|
|
|
|
You probably won't need to do this very often, but, when you do, it's
|
|
|
|
possible.
|
|
|
|
|
2009-03-24 07:25:03 +08:00
|
|
|
.. _proxy-vs-unmanaged-models:
|
|
|
|
|
2016-05-14 03:18:33 +08:00
|
|
|
Differences between proxy inheritance and unmanaged models
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2009-03-19 17:04:19 +08:00
|
|
|
|
|
|
|
Proxy model inheritance might look fairly similar to creating an unmanaged
|
|
|
|
model, using the :attr:`~django.db.models.Options.managed` attribute on a
|
2016-02-19 03:27:55 +08:00
|
|
|
model's ``Meta`` class.
|
|
|
|
|
|
|
|
With careful setting of :attr:`Meta.db_table
|
|
|
|
<django.db.models.Options.db_table>` you could create an unmanaged model that
|
|
|
|
shadows an existing model and adds Python methods to it. However, that would be
|
|
|
|
very repetitive and fragile as you need to keep both copies synchronized if you
|
2009-03-19 17:04:19 +08:00
|
|
|
make any changes.
|
|
|
|
|
2016-02-19 03:27:55 +08:00
|
|
|
On the other hand, proxy models are intended to behave exactly like the model
|
|
|
|
they are proxying for. They are always in sync with the parent model since they
|
|
|
|
directly inherit its fields and managers.
|
|
|
|
|
|
|
|
The general rules are:
|
2009-03-19 17:04:19 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
1. If you are mirroring an existing model or database table and don't want
|
|
|
|
all the original database table columns, use ``Meta.managed=False``.
|
|
|
|
That option is normally useful for modeling database views and tables
|
|
|
|
not under the control of Django.
|
|
|
|
2. If you are wanting to change the Python-only behavior of a model, but
|
|
|
|
keep all the same fields as in the original, use ``Meta.proxy=True``.
|
|
|
|
This sets things up so that the proxy model is an exact copy of the
|
|
|
|
storage structure of the original model when data is saved.
|
2009-03-19 17:04:19 +08:00
|
|
|
|
2014-04-28 21:28:03 +08:00
|
|
|
.. _model-multiple-inheritance-topic:
|
|
|
|
|
2008-08-31 18:13:32 +08:00
|
|
|
Multiple inheritance
|
|
|
|
--------------------
|
|
|
|
|
|
|
|
Just as with Python's subclassing, it's possible for a Django model to inherit
|
|
|
|
from multiple parent models. Keep in mind that normal Python name resolution
|
2008-09-07 00:51:24 +08:00
|
|
|
rules apply. The first base class that a particular name (e.g. :ref:`Meta
|
|
|
|
<meta-options>`) appears in will be the one that is used; for example, this
|
|
|
|
means that if multiple parents contain a :ref:`Meta <meta-options>` class,
|
|
|
|
only the first one is going to be used, and all others will be ignored.
|
2008-08-31 18:13:32 +08:00
|
|
|
|
|
|
|
Generally, you won't need to inherit from multiple parents. The main use-case
|
|
|
|
where this is useful is for "mix-in" classes: adding a particular extra
|
|
|
|
field or method to every class that inherits the mix-in. Try to keep your
|
|
|
|
inheritance hierarchies as simple and straightforward as possible so that you
|
|
|
|
won't have to struggle to work out where a particular piece of information is
|
|
|
|
coming from.
|
2009-03-04 18:39:29 +08:00
|
|
|
|
2015-01-27 04:39:52 +08:00
|
|
|
Note that inheriting from multiple models that have a common ``id`` primary
|
|
|
|
key field will raise an error. To properly use multiple inheritance, you can
|
|
|
|
use an explicit :class:`~django.db.models.AutoField` in the base models::
|
2014-04-28 21:28:03 +08:00
|
|
|
|
|
|
|
class Article(models.Model):
|
|
|
|
article_id = models.AutoField(primary_key=True)
|
|
|
|
...
|
|
|
|
|
|
|
|
class Book(models.Model):
|
|
|
|
book_id = models.AutoField(primary_key=True)
|
|
|
|
...
|
|
|
|
|
|
|
|
class BookReview(Book, Article):
|
|
|
|
pass
|
|
|
|
|
|
|
|
Or use a common ancestor to hold the :class:`~django.db.models.AutoField`::
|
|
|
|
|
|
|
|
class Piece(models.Model):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class Article(Piece):
|
|
|
|
...
|
|
|
|
|
|
|
|
class Book(Piece):
|
|
|
|
...
|
|
|
|
|
|
|
|
class BookReview(Book, Article):
|
|
|
|
pass
|
|
|
|
|
2009-03-04 18:39:29 +08:00
|
|
|
Field name "hiding" is not permitted
|
2017-03-21 06:30:32 +08:00
|
|
|
------------------------------------
|
2009-03-04 18:39:29 +08:00
|
|
|
|
|
|
|
In normal Python class inheritance, it is permissible for a child class to
|
2015-08-03 20:03:41 +08:00
|
|
|
override any attribute from the parent class. In Django, this isn't usually
|
|
|
|
permitted for model fields. If a non-abstract model base class has a field
|
|
|
|
called ``author``, you can't create another model field or define
|
|
|
|
an attribute called ``author`` in any class that inherits from that base class.
|
|
|
|
|
|
|
|
This restriction doesn't apply to model fields inherited from an abstract
|
|
|
|
model. Such fields may be overridden with another field or value, or be removed
|
|
|
|
by setting ``field_name = None``.
|
|
|
|
|
|
|
|
.. warning::
|
|
|
|
|
|
|
|
Model managers are inherited from abstract base classes. Overriding an
|
|
|
|
inherited field which is referenced by an inherited
|
|
|
|
:class:`~django.db.models.Manager` may cause subtle bugs. See :ref:`custom
|
|
|
|
managers and model inheritance <custom-managers-and-inheritance>`.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
Some fields define extra attributes on the model, e.g. a
|
|
|
|
:class:`~django.db.models.ForeignKey` defines an extra attribute with
|
|
|
|
``_id`` appended to the field name, as well as ``related_name`` and
|
|
|
|
``related_query_name`` on the foreign model.
|
|
|
|
|
|
|
|
These extra attributes cannot be overridden unless the field that defines
|
|
|
|
it is changed or removed so that it no longer defines the extra attribute.
|
2009-03-04 18:39:29 +08:00
|
|
|
|
|
|
|
Overriding fields in a parent model leads to difficulties in areas such as
|
2011-05-13 12:33:42 +08:00
|
|
|
initializing new instances (specifying which field is being initialized in
|
2009-03-04 18:39:29 +08:00
|
|
|
``Model.__init__``) and serialization. These are features which normal Python
|
|
|
|
class inheritance doesn't have to deal with in quite the same way, so the
|
|
|
|
difference between Django model inheritance and Python class inheritance isn't
|
2010-11-06 11:26:17 +08:00
|
|
|
arbitrary.
|
2009-03-04 18:39:29 +08:00
|
|
|
|
|
|
|
This restriction only applies to attributes which are
|
2011-03-05 06:29:16 +08:00
|
|
|
:class:`~django.db.models.Field` instances. Normal Python attributes
|
2009-03-04 18:39:29 +08:00
|
|
|
can be overridden if you wish. It also only applies to the name of the
|
|
|
|
attribute as Python sees it: if you are manually specifying the database
|
|
|
|
column name, you can have the same column name appearing in both a child and
|
|
|
|
an ancestor model for multi-table inheritance (they are columns in two
|
|
|
|
different database tables).
|
|
|
|
|
2010-12-06 07:52:23 +08:00
|
|
|
Django will raise a :exc:`~django.core.exceptions.FieldError` if you override
|
|
|
|
any model field in any ancestor model.
|
2014-09-28 06:12:34 +08:00
|
|
|
|
2016-06-04 02:15:32 +08:00
|
|
|
Organizing models in a package
|
|
|
|
==============================
|
|
|
|
|
|
|
|
The :djadmin:`manage.py startapp <startapp>` command creates an application
|
|
|
|
structure that includes a ``models.py`` file. If you have many models,
|
|
|
|
organizing them in separate files may be useful.
|
|
|
|
|
|
|
|
To do so, create a ``models`` package. Remove ``models.py`` and create a
|
|
|
|
``myapp/models/`` directory with an ``__init__.py`` file and the files to
|
|
|
|
store your models. You must import the models in the ``__init__.py`` file.
|
|
|
|
|
|
|
|
For example, if you had ``organic.py`` and ``synthetic.py`` in the ``models``
|
|
|
|
directory:
|
|
|
|
|
|
|
|
.. snippet::
|
|
|
|
:filename: myapp/models/__init__.py
|
|
|
|
|
|
|
|
from .organic import Person
|
|
|
|
from .synthetic import Robot
|
|
|
|
|
|
|
|
Explicitly importing each model rather than using ``from .models import *``
|
|
|
|
has the advantages of not cluttering the namespace, making code more readable,
|
|
|
|
and keeping code analysis tools useful.
|
|
|
|
|
2014-09-28 06:12:34 +08:00
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
:doc:`The Models Reference </ref/models/index>`
|
|
|
|
Covers all the model related APIs including model fields, related
|
|
|
|
objects, and ``QuerySet``.
|