2005-07-13 09:25:57 +08:00
|
|
|
===============
|
|
|
|
Model reference
|
|
|
|
===============
|
|
|
|
|
2005-07-17 12:20:57 +08:00
|
|
|
Django's models are the bread and butter of the framework. There's a huge
|
|
|
|
array of options available to you when defining your data models. This
|
|
|
|
document explains them.
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
Options for models
|
|
|
|
==================
|
|
|
|
|
2005-07-17 12:20:57 +08:00
|
|
|
A list of all possible options for a model object follows. Although there's a
|
|
|
|
wide array of options, only ``fields`` is required.
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
``admin``
|
2005-07-15 08:42:28 +08:00
|
|
|
A ``meta.Admin`` object; see `Admin options`_. If this field isn't given,
|
|
|
|
the object will not have an admin interface.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
``db_table``
|
2005-07-15 08:42:28 +08:00
|
|
|
The name of the database table to use for the module::
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
db_table = "pizza_orders"
|
2005-07-17 12:20:57 +08:00
|
|
|
|
|
|
|
If this isn't given, Django will use ``app_label + '_' + module_name``.
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
``exceptions``
|
2005-07-15 08:42:28 +08:00
|
|
|
Names of extra exception subclasses to include in the generated module.
|
|
|
|
These exceptions are available from instance methods and from module-level
|
|
|
|
methods::
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
exceptions = ("DisgustingToppingsException", "BurntCrust")
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
``fields``
|
2005-07-17 12:20:57 +08:00
|
|
|
A list of field objects. See `Field objects`_. For example::
|
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
fields = (
|
|
|
|
meta.CharField('customer_name', 'customer name', maxlength=15),
|
|
|
|
meta.BooleanField('use_extra_cheese', 'use extra cheese'),
|
|
|
|
meta.IntegerField('customer_type', 'customer type', choices=CUSTOMER_TYPE_CHOICES),
|
|
|
|
...
|
|
|
|
)
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
``get_latest_by``
|
2005-07-17 12:20:57 +08:00
|
|
|
The name of a ``DateField`` or ``DateTimeField``; if given, the module will
|
|
|
|
have a ``get_latest()`` function that fetches the "latest" object according
|
|
|
|
to that field::
|
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
get_latest_by = "order_date"
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
``module_constants``
|
2005-07-17 12:20:57 +08:00
|
|
|
A dictionary of names/values to use as extra module-level constants::
|
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
module_constants = {
|
|
|
|
'MEAT_TYPE_PEPPERONI' : 1,
|
|
|
|
'MEAT_TYPE_SAUSAGE' : 2,
|
|
|
|
}
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
``module_name``
|
2005-07-15 08:42:28 +08:00
|
|
|
The name of the module::
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
module_name = "pizza_orders"
|
2005-07-17 12:20:57 +08:00
|
|
|
|
|
|
|
If this isn't given, Django will use a lowercased version of the class
|
|
|
|
name, plus "s".
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
``order_with_respect_to``
|
2005-07-15 08:42:28 +08:00
|
|
|
Marks this object as "orderable" with respect to the given field. This is
|
|
|
|
almost always used with related objects to allow them to be ordered with
|
|
|
|
respect to a parent object. For example, if a ``PizzaToppping`` relates to
|
|
|
|
a ``Pizza`` object, you might use::
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
order_with_respect_to = 'pizza_id'
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
to allow the toppings to be ordered with respect to the associated pizza.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
``ordering``
|
2005-07-17 12:20:57 +08:00
|
|
|
The default ordering for the object, for use by ``get_list`` and the admin::
|
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
ordering = (('order_date', 'DESC'),)
|
2005-07-17 12:20:57 +08:00
|
|
|
|
|
|
|
This is a tuple of 2-tuples. Each 2-tuple is ``(field_name, ordering_type)``
|
|
|
|
where ordering_type is either ``"ASC"`` or ``"DESC"``. You can also use the
|
|
|
|
``(None, "RANDOM")`` for random ordering.
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
``permissions``
|
2005-07-15 08:42:28 +08:00
|
|
|
Extra permissions to enter into the permissions table when creating this
|
2005-07-17 12:20:57 +08:00
|
|
|
object. A add, delete, and change permission is automatically created for
|
|
|
|
each object. This option specifies extra permissions::
|
|
|
|
|
|
|
|
permissions = (("can_delivier_pizzas", "Can deliver pizzas"),)
|
|
|
|
|
|
|
|
This is a list of 2-tuples of
|
2005-07-15 08:42:28 +08:00
|
|
|
``(permission_code, human_readable_permission_name)``.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
``unique_together``
|
2005-07-15 08:42:28 +08:00
|
|
|
Sets of field names that, taken together, must be unique::
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
unique_together = (("driver_id", "restaurant_id"),)
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
This is a list of lists of fields that must be unique when considered
|
2005-07-17 12:20:57 +08:00
|
|
|
together. It's used in the Django admin.
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
``verbose_name``
|
2005-07-15 08:42:28 +08:00
|
|
|
A human-readable name for the object, singular::
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
verbose_name = "pizza"
|
2005-07-17 12:20:57 +08:00
|
|
|
|
|
|
|
If this isn't given, Django will use a munged version of the class name:
|
2005-07-15 08:42:28 +08:00
|
|
|
``CamelCase`` becomes ``camel case``.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
``verbose_name_plural``
|
2005-07-15 08:42:28 +08:00
|
|
|
The plural name for the object::
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
verbose_name_plural = "stories"
|
2005-07-17 12:20:57 +08:00
|
|
|
|
|
|
|
If this isn't given, Django will use ``verbose_name + "s"``.
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
Field objects
|
|
|
|
=============
|
|
|
|
|
|
|
|
The list of fields is the most important part of a data model. Each item in
|
2005-07-17 12:20:57 +08:00
|
|
|
the ``fields`` list is an instance of a ``meta.Field`` subclass and maps to
|
2005-07-13 09:25:57 +08:00
|
|
|
a database field.
|
|
|
|
|
|
|
|
All field objects -- except for ``ForeignKey`` and ``ManyToManyField`` (see
|
|
|
|
below) -- take two positional arguments and a number of keyword arguments.
|
|
|
|
The positional arguments are the field name and the human-readable name. The
|
|
|
|
field name must be a valid Python identifier, but the human-readable name can
|
|
|
|
contain spaces, punctuation, etc.
|
|
|
|
|
|
|
|
General field options
|
|
|
|
---------------------
|
|
|
|
|
2005-07-17 12:20:57 +08:00
|
|
|
Each type of field takes a different set of options, but some options are
|
|
|
|
common to all field types. These options are:
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-07-15 08:53:18 +08:00
|
|
|
====================== ===================================================
|
2005-07-13 09:25:57 +08:00
|
|
|
Option Description
|
2005-07-15 08:53:18 +08:00
|
|
|
====================== ===================================================
|
2005-07-17 12:20:57 +08:00
|
|
|
``blank`` If ``True``, the field is allowed to be blank.
|
2005-07-13 09:25:57 +08:00
|
|
|
Note that this is different from ``null`` in that
|
|
|
|
string fields will store the empty string instead of
|
2005-07-17 12:20:57 +08:00
|
|
|
``NULL`` internally. This means that to create a
|
2005-07-13 09:25:57 +08:00
|
|
|
field that stores nulls you must pass ``blank=True``
|
2005-07-17 12:20:57 +08:00
|
|
|
and ``null=True``.
|
|
|
|
|
|
|
|
``choices`` A list of 2-tuples to use as choices for this
|
|
|
|
field. If this is given, Django's admin will use a
|
|
|
|
select box instead of the standard text field and
|
|
|
|
will limit choices to the choices given. A choices
|
|
|
|
list looks like::
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
YEAR_IN_SCHOOL_CHOICES = (
|
|
|
|
('FR', 'Freshman'),
|
|
|
|
('SO', 'Sophomore'),
|
|
|
|
('JR', 'Junior'),
|
|
|
|
('SR', 'Senior'),
|
|
|
|
('GR', 'Graduate'),
|
|
|
|
)
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
The first element in each tuple is the actual value
|
2005-07-17 12:20:57 +08:00
|
|
|
to be stored. The second element is the
|
|
|
|
human-readable name for the option.
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
``core`` For objects that are edited inline to a related
|
2005-07-15 08:48:28 +08:00
|
|
|
object. If all "core" fields in an inline-edited
|
|
|
|
object are cleared, the object will be considered to
|
|
|
|
be deleted.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
It is an error to have an inline-editable
|
|
|
|
relation without at least one core field.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
``db_index`` If ``True``, the SQL generator will create a database
|
|
|
|
index on this field.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
``default`` The default value for the field.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
|
|
|
``editable`` ``True`` by default. If this is set to ``False``,
|
|
|
|
the field will not be editable in the admin.
|
|
|
|
|
|
|
|
``help_text`` Extra "help" text to be displayed under the field
|
|
|
|
on the object's admin form.
|
|
|
|
|
|
|
|
``null`` If ``True``, empty values in the field will be
|
|
|
|
stored as ``NULL`` in the database.
|
|
|
|
|
|
|
|
``primary_key`` If ``True``, this field is the primary key for the
|
|
|
|
table. You only need to use this if you don't want
|
2005-07-13 09:25:57 +08:00
|
|
|
the standard "id" field created and used as the
|
|
|
|
primary key.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
Implies ``blank=False``, ``null=False``, and
|
2005-07-17 12:20:57 +08:00
|
|
|
``unique=True``. Only one primary key is allowed
|
|
|
|
on an object.
|
|
|
|
|
|
|
|
``radio_admin`` If ``choices`` is given, or if the field is a
|
|
|
|
ManyToOne relation, use a radio-button interface
|
|
|
|
for the choices instead of the standard select-box
|
|
|
|
interface.
|
|
|
|
|
|
|
|
``unique`` If ``True``, this field must be unique throughout
|
|
|
|
the table. This is enforced at the database level
|
|
|
|
and at the Django admin-form level.
|
|
|
|
|
|
|
|
``unique_for_date`` Set this to the name of a ``DateField`` or
|
2005-07-13 09:25:57 +08:00
|
|
|
``DateTimeField`` to require that this field
|
2005-07-17 12:20:57 +08:00
|
|
|
be unique for the value of the date field. For
|
|
|
|
example, if you have a field ``title`` that has
|
2005-07-13 09:25:57 +08:00
|
|
|
``unique_for_date="pub_date"``, then it is an
|
|
|
|
error to have two rows with the same ``title``
|
|
|
|
and the same ``pub_date``.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
``unique_for_month`` Like ``unique_for_date``, but requires the field
|
|
|
|
to be unique with respect to the month.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
|
|
|
``unique_for_year`` Like ``unique_for_date`` and ``unique_for_month``.
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
``validator_list`` A list of extra validators to apply to the field.
|
2005-07-15 08:53:18 +08:00
|
|
|
====================== ===================================================
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
Field Types
|
|
|
|
-----------
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
``AutoField``
|
2005-07-17 12:20:57 +08:00
|
|
|
An ``IntegerField`` that automatically increments. You usually won't need to
|
|
|
|
use this directly; a primary key field will automatically be added to your
|
|
|
|
model if you don't specify otherwise. That automatically-added field is::
|
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
meta.AutoField('id', 'ID', primary_key=True)
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
``BooleanField``
|
2005-07-15 08:42:28 +08:00
|
|
|
A true/false field.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
``CharField``
|
2005-07-17 12:20:57 +08:00
|
|
|
A text field. These are displayed in the admin as single-line text inputs, so
|
|
|
|
for large amounts of text, use a ``TextField``.
|
|
|
|
|
|
|
|
``CharField``s have an extra required argument: ``maxlength``, the maximum
|
|
|
|
length (in characters) of the field. The maxlength is enforced at the database
|
|
|
|
level and in Django's admin validation.
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
``CommaSeparatedIntegerField``
|
2005-07-15 08:42:28 +08:00
|
|
|
A field of integers separated by commas.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
``DateField``
|
2005-07-17 12:20:57 +08:00
|
|
|
A date field. Has a few extra optional options:
|
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
====================== ===================================================
|
|
|
|
Option Description
|
|
|
|
====================== ===================================================
|
|
|
|
``auto_now`` Automatically set the field to now every time the
|
2005-07-17 12:20:57 +08:00
|
|
|
object is saved. Useful for "last-modified"
|
2005-07-15 08:42:28 +08:00
|
|
|
timestamps.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
``auto_now_add`` Automatically set the field to now when the object
|
2005-07-17 12:20:57 +08:00
|
|
|
is first created. Useful for creation of
|
|
|
|
timestamps.
|
2005-07-15 08:42:28 +08:00
|
|
|
====================== ===================================================
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
``DateTimeField``
|
2005-07-15 08:42:28 +08:00
|
|
|
A date and time field. Takes the same extra options as ``DateField``.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
``EmailField``
|
2005-07-17 12:20:57 +08:00
|
|
|
A ``CharField`` that checks that the value is a valid e-mail address.
|
|
|
|
Because validating e-mail addresses can be tricky, this is a pretty loose
|
|
|
|
test.
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
``FileField``
|
2005-07-17 12:20:57 +08:00
|
|
|
A file-upload field. Takes an additional option, ``upload_to``, which is
|
|
|
|
a local filesystem path to upload the file to. This path may contain
|
|
|
|
`strftime formatting`_, which will be replaced by the date/time of the file
|
|
|
|
upload (so that uploaded files don't fill up the given directory).
|
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
.. _`strftime formatting`: http://docs.python.org/lib/module-time.html#l2h-1941
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
``FloatField``
|
2005-07-17 12:20:57 +08:00
|
|
|
A floating-point number. Has two additional required options:
|
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
====================== ===================================================
|
|
|
|
Option Description
|
|
|
|
====================== ===================================================
|
|
|
|
``max_digits`` The maximum number of digits allowed in the number.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
``decimal_places`` The number of decimal places to store with the
|
|
|
|
number
|
|
|
|
====================== ===================================================
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
For example, to store numbers up to 999 with a resolution of 2 decimal places,
|
|
|
|
you'd use::
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
meta.FloatField(..., max_digits=5, decimal_places=2)
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
And to store numbers up to one million with a resolution of 10 decimal places::
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
meta.FloatField(..., max_digits=19, decimal_places=10)
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
``ForeignKey``
|
2005-07-17 12:20:57 +08:00
|
|
|
A many-to-one relationship to the primary key in another object. So, to give a
|
|
|
|
``Topping`` object a many-to-one relationship to ``Pizza`` (i.e. there are
|
2005-07-15 08:42:28 +08:00
|
|
|
many toppings on a pizza)::
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
meta.ForeignKey(Pizza)
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-16 04:37:03 +08:00
|
|
|
``ForeignKey`` fields take a large number of extra options for defining how
|
|
|
|
the relationship should work:
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-15 08:54:49 +08:00
|
|
|
======================= ============================================================
|
2005-07-13 09:25:57 +08:00
|
|
|
Option Description
|
2005-07-15 08:54:49 +08:00
|
|
|
======================= ============================================================
|
2005-07-17 12:20:57 +08:00
|
|
|
``edit_inline`` If ``True``, this related object is edited
|
|
|
|
"inline" on the related object's page. This means
|
|
|
|
that the object will not have its own admin
|
|
|
|
interface.
|
|
|
|
|
|
|
|
``edit_inline_type`` This is either ``meta.TABULAR`` or
|
|
|
|
``meta.STACKED`` and controls whether the inline
|
2005-07-13 09:25:57 +08:00
|
|
|
editable objects are displayed as a table or as
|
2005-07-17 12:20:57 +08:00
|
|
|
a "stack" of fieldsets. Defaults to
|
2005-07-13 09:25:57 +08:00
|
|
|
``meta.STACKED``.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
``limit_choices_to`` A dictionary of lookup arguments and values (see
|
2005-07-17 12:20:57 +08:00
|
|
|
the `Database API reference`_) that limit the
|
|
|
|
available admin choices for this object. Use this
|
|
|
|
with ``meta.LazyDate`` to limit choices of objects
|
|
|
|
by date. For example::
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
limit_choices_to = {'pub_date__lte' : meta.LazyDate()}
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
only allows the choice of related objects with a
|
|
|
|
``pub_date`` before the current date/time to be
|
|
|
|
chosen.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
Not compatible with ``edit_inline``.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
``max_num_in_admin`` For inline-edited objects, this is the maximum
|
|
|
|
number of related objects to display in the admin.
|
2005-07-17 12:20:57 +08:00
|
|
|
Thus, if a pizza could only have up to 10
|
2005-07-13 09:25:57 +08:00
|
|
|
toppings, ``max_num_in_admin=10`` would ensure
|
|
|
|
that a user never enters more than 10 toppings.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
Note that this doesn't ensure more than 10 related
|
2005-07-17 12:20:57 +08:00
|
|
|
toppings ever get created. It just controls the
|
|
|
|
interface.
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
``min_num_in_admin`` The minimum number of related objects displayed in
|
2005-07-17 12:20:57 +08:00
|
|
|
the admin. Normally, at the creation stage,
|
2005-07-13 09:25:57 +08:00
|
|
|
``num_in_admin`` inline objects are shown, and at
|
2005-07-17 12:20:57 +08:00
|
|
|
the edit stage ``num_extra_on_change`` blank
|
|
|
|
objects are shown in addition to all pre-existing
|
|
|
|
related objects. However, no fewer than
|
2005-07-13 09:25:57 +08:00
|
|
|
``min_num_in_admin`` related objects will ever be
|
|
|
|
displayed.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
|
|
|
``num_extra_on_change`` The number of extra blank related-object fields to
|
2005-07-13 09:25:57 +08:00
|
|
|
show at the change stage.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
``num_in_admin`` The default number of inline objects to display
|
|
|
|
on the object page at the add stage.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
``raw_id_admin`` Only display a field for the integer to be entered
|
2005-07-17 12:20:57 +08:00
|
|
|
instead of a drop-down menu. This is useful when
|
2005-07-13 09:25:57 +08:00
|
|
|
related to an object type that will have too many
|
2005-07-17 12:20:57 +08:00
|
|
|
rows to make a select box practical.
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
Not used with ``edit_inline``.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
|
|
|
``rel_name`` The name of the relation. In the above example,
|
|
|
|
this would default to 'pizza' (so that the
|
2005-07-15 08:42:28 +08:00
|
|
|
``Toppings`` object would have a ``get_pizza()``
|
2005-07-17 12:20:57 +08:00
|
|
|
function. If you set ``rel_name`` to "pie", then
|
2005-07-15 08:42:28 +08:00
|
|
|
the function would be called ``get_pie()`` and the
|
|
|
|
field name would be ``pie_id``.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
``related_name`` The name to use for the relation from the related
|
|
|
|
object back to this one. For example, when if
|
|
|
|
``Topping`` has this field::
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
meta.ForeignKey(Pizza)
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
the ``related_name`` will be "topping" (taken from
|
|
|
|
the class name which will in turn give ``Pizza``
|
2005-07-17 12:20:57 +08:00
|
|
|
methods like ``get_topping_list()`` and
|
|
|
|
``get_topping_count()``.
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
If you instead were to use::
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
meta.ForeignKey(Pizza, related_name="munchie")
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
then the methods would be called
|
|
|
|
``get_munchie_list()``, ``get_munchie_count()``,
|
|
|
|
etc.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
This is only really useful when you have a single
|
|
|
|
object that relates to the same object more than
|
|
|
|
once. For example, if a ``Story`` object has both
|
2005-07-17 12:20:57 +08:00
|
|
|
``primary_category`` and ``secondary_category``
|
2005-07-13 09:25:57 +08:00
|
|
|
fields, to make sure that the category objects
|
|
|
|
have the correct methods, you'd use fields like::
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
...
|
|
|
|
meta.ForeignKey(Category, name="primary_category_id",
|
|
|
|
rel_name="primary_category",
|
|
|
|
related_name="primary_story"),
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
meta.ForeignKey(Category, name="secondary_category_id",
|
|
|
|
rel_name="secondary_category",
|
|
|
|
related_name="secondary_story"),
|
|
|
|
...
|
2005-07-17 12:20:57 +08:00
|
|
|
|
|
|
|
which would give the category objects methods
|
|
|
|
named ``get_primary_story_list()`` and
|
2005-07-13 09:25:57 +08:00
|
|
|
``get_secondary_story_list()``.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
``to_field`` The field on the related object that the relation
|
2005-07-17 12:20:57 +08:00
|
|
|
is to. This is almost always ``id``, but if the
|
|
|
|
primary key on the other object is named something
|
2005-07-15 08:42:28 +08:00
|
|
|
different, this is how to indicate that.
|
2005-07-15 08:54:49 +08:00
|
|
|
======================= ============================================================
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-07-17 12:20:57 +08:00
|
|
|
.. _`Database API reference`: http://www.djangoproject.com/documentation/db_api/
|
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
``ImageField``
|
2005-07-17 12:20:57 +08:00
|
|
|
Like a ``FieldField``, but validates that the uploaded object is a valid
|
|
|
|
image. Has two extra optional arguments, ``height_field`` and ``width_field``
|
2005-07-15 08:42:28 +08:00
|
|
|
which, if set, will be auto-populated with the height and width of the image.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
|
|
|
Requires the `Python Imaging Library`_.
|
|
|
|
|
|
|
|
.. _Python Imaging Library: http://www.pythonware.com/products/pil/
|
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
``IntegerField``
|
2005-07-17 12:20:57 +08:00
|
|
|
An integer.
|
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
``IPAddressField``
|
|
|
|
An IP address, in string format (i.e. "24.124.1.30").
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
``ManyToManyField``
|
2005-07-16 04:37:03 +08:00
|
|
|
A many-to-many relation to another object. For example (taken from the
|
|
|
|
``core.flatfiles`` object::
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-16 04:37:03 +08:00
|
|
|
class FlatFile(meta.Model):
|
|
|
|
fields = (
|
|
|
|
...
|
|
|
|
meta.ManyToManyField(Site),
|
|
|
|
)
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-16 04:37:03 +08:00
|
|
|
Many-to-many relations are a bit different from other fields. First, they
|
2005-07-17 12:20:57 +08:00
|
|
|
aren't actually a field per se, because they use a intermediary join table.
|
|
|
|
Second, they don't take any of the same options as the rest of the fields.
|
|
|
|
The only options taken are:
|
|
|
|
|
2005-07-16 04:37:03 +08:00
|
|
|
======================= ============================================================
|
|
|
|
Option Description
|
|
|
|
======================= ============================================================
|
2005-07-17 12:20:57 +08:00
|
|
|
``related_name`` See the description of ``related_name`` in
|
2005-07-16 04:37:03 +08:00
|
|
|
``ManyToOneField``, above.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
|
|
|
``filter_interface`` Use a nifty unobtrusive Javascript "filter" interface
|
|
|
|
instead of the usability-challenged ``<select multiple>``
|
|
|
|
in the admin form for this object. The value should be
|
|
|
|
``meta.HORIZONTAL`` or ``meta.VERTICAL`` (i.e.
|
|
|
|
should the interface be stacked horizontally or
|
2005-07-16 04:37:03 +08:00
|
|
|
vertically).
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-16 04:37:03 +08:00
|
|
|
``limit_choices_to`` See the description under ``ManyToOneField``, above.
|
|
|
|
======================= ============================================================
|
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
``NullBooleanField``
|
|
|
|
Like a ``BooleanField``, but allows ``NULL`` as one of the options. Use this
|
2005-07-17 12:20:57 +08:00
|
|
|
instead of a ``BooleanField`` with ``null=True``.
|
|
|
|
|
2005-07-16 04:37:03 +08:00
|
|
|
``OneToOneField``
|
|
|
|
Signifies a one-to-one relationship. This is most useful on the primary key
|
2005-07-17 12:20:57 +08:00
|
|
|
of an object when that object "extends" another object in some way.
|
|
|
|
|
2005-07-16 04:37:03 +08:00
|
|
|
For example, if you are building a database of "places", you would build pretty
|
|
|
|
standard stuff like address, phone number, etc. in the database. If you then
|
|
|
|
wanted to build a database of restaurants on top of the places, instead of
|
|
|
|
repeating yourself and replicating those fields in the restaurants object, you
|
|
|
|
could make ``Restaurant`` have a ``OneToOneField`` to ``Place`` (since
|
|
|
|
a restaurant "is-a" place). This ``OneToOneField`` will actually replace
|
|
|
|
the primary key ``id`` field (since one-to-one relations share the same
|
|
|
|
primary key), and has a few in the admin interface:
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-16 04:37:03 +08:00
|
|
|
* No selection interface is displayed on ``Restaurant`` pages; there will
|
|
|
|
be one (and only one) ``Restaurant`` for each place.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-16 04:37:03 +08:00
|
|
|
* On the ``Restaurant`` change list, every single ``Place`` -- weather it
|
2005-07-17 12:20:57 +08:00
|
|
|
has an associated ``Restaurant`` or not -- will be displayed. Adding
|
2005-07-16 04:37:03 +08:00
|
|
|
a ``Restaurant`` to a ``Place`` just means filling out the required
|
|
|
|
``Restaurant`` fields.
|
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
``PhoneNumberField``
|
|
|
|
Validates that the value is a valid phone number.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
``PositiveIntegerField``
|
|
|
|
Like an ``IntegerField``, but must be positive.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
``PositiveSmallIntegerField``
|
2005-07-17 12:20:57 +08:00
|
|
|
Like a ``PositiveIntegerField``, but only allows values under a certain
|
|
|
|
(database-dependent) point.
|
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
``SlugField``
|
2005-07-17 12:20:57 +08:00
|
|
|
A "slug," suitable for parts of a URL. Only allows alphanumeric characters
|
|
|
|
and underscores.
|
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
Implies ``maxlength=50`` and ``db_index=True``.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
|
|
|
Accepts an extra option, ``prepopulate_from``, which is a list of fields
|
|
|
|
from which to auto-populate the slug, via JavaScript, in the object's admin
|
|
|
|
form.
|
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
``SmallIntegerField``
|
2005-07-17 12:20:57 +08:00
|
|
|
Like an ``IntegerField``, but only allows values under a certain
|
|
|
|
(database-dependent) point.
|
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
``TextField``
|
|
|
|
A large text field (``<textarea>`` in HTML).
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
``TimeField``
|
2005-07-17 12:20:57 +08:00
|
|
|
A time. Accepts the same auto-population options as ``DateField`` and
|
2005-07-15 08:42:28 +08:00
|
|
|
``DateTimeField``.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
``URLField``
|
2005-07-17 12:20:57 +08:00
|
|
|
A field for a URL. If the ``verify_exists`` option is ``True`` (default),
|
|
|
|
the URL given will be checked for existence (i.e., the URL actually loads
|
|
|
|
and doesn't give a 404 response).
|
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
``USStateField``
|
2005-07-17 12:20:57 +08:00
|
|
|
A two-letter U.S. state abbreviation.
|
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
``XMLField``
|
2005-07-17 12:20:57 +08:00
|
|
|
A field containing XML. Takes one required argument, ``schema_path``, which
|
|
|
|
is the filesystem path to a RelaxNG_ schema against which to validate the
|
|
|
|
field.
|
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
.. _RelaxNG: http://www.relaxng.org/
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
Admin options
|
|
|
|
=============
|
|
|
|
|
|
|
|
The ``admin`` field in the model tells Django how to construct the admin
|
2005-07-17 12:20:57 +08:00
|
|
|
interface for the object. The field is an instance of the ``meta.Admin``
|
2005-07-13 09:25:57 +08:00
|
|
|
object, which has the following options (of which only ``fields`` is required):
|
|
|
|
|
2005-07-17 12:20:57 +08:00
|
|
|
``date_hierarchy``
|
2005-07-15 08:42:28 +08:00
|
|
|
To allow filtering of objects in the admin by date, set ``date_hierarchy``
|
|
|
|
to the name of the field to filter by::
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
date_hierarchy = 'order_date'
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
``fields``
|
2005-07-15 08:42:28 +08:00
|
|
|
A list of fieldsets to display on the admin page. Each fieldset is a 2-tuple:
|
|
|
|
``(name, field_options)``. The ``name`` is a string to name the field set,
|
2005-07-17 12:20:57 +08:00
|
|
|
and ``field_options`` is a dictionary of information about the fields to be
|
|
|
|
displayed in that fieldset. This dictionary has the following keys:
|
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
``fields``
|
2005-07-17 12:20:57 +08:00
|
|
|
A tuple of field names to display in this fieldset. To display
|
2005-07-15 08:42:28 +08:00
|
|
|
multiple fields on the same line, wrap those fields in their
|
|
|
|
own tuple.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
This key is required in the dict.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
``classes``
|
2005-07-17 12:20:57 +08:00
|
|
|
Extra CSS classes to apply to the fieldset. This is a simple
|
|
|
|
string. You can apply multiple classes by separating them with
|
2005-07-15 08:42:28 +08:00
|
|
|
spaces.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
|
|
|
Two useful classes defined by the default stylesheet are
|
|
|
|
``collapse`` and ``wide``. Fieldsets with the ``collapse`` style
|
|
|
|
will be initially collapsed in the admin and replaced with a small
|
|
|
|
"click to expand" link. Fieldsets with the ``wide`` style will be
|
|
|
|
given extra horizontal space.
|
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
For example (taken from the ``core.flatfiles`` model)::
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
fields = (
|
|
|
|
(None, {
|
|
|
|
'fields': ('url', 'title', 'content', 'sites')
|
|
|
|
}),
|
|
|
|
('Advanced options', {
|
2005-07-17 12:20:57 +08:00
|
|
|
'classes': 'collapse',
|
2005-07-15 08:42:28 +08:00
|
|
|
'fields' : ('enable_comments', 'registration_required', 'template_name')
|
|
|
|
}),
|
|
|
|
),
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
results in an admin that looks like:
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
.. image:: http://media.djangoproject.com/img/doc/flatfiles_admin.png
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
``js``
|
2005-07-17 12:20:57 +08:00
|
|
|
A list of strings representing URLs of JavaScript files to link into the
|
|
|
|
admin screen. This can be used to tweak a given type of admin page in JS or
|
|
|
|
to provide "quick links" to fill in default values for certain fields.
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
``list_display``
|
2005-07-15 08:42:28 +08:00
|
|
|
List of fields to display on the list page in the admin.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
There are a few special cases that do other things besides displaying the
|
|
|
|
contents of the given fields:
|
2005-07-17 12:20:57 +08:00
|
|
|
|
|
|
|
* If the field given has a relationship, that relationship is
|
2005-07-15 08:42:28 +08:00
|
|
|
followed and the ``repr()`` of the related object is displayed.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
* If the field is a ``BooleanField``, a "on" or "off" icon will
|
|
|
|
be displayed instead of ``True`` or ``False``.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
|
|
|
* If the field name is a method of the model, it'll be called, and the
|
|
|
|
output will be displayed. This method should have a
|
|
|
|
``short_description`` function attribute, for use as the header for
|
|
|
|
the field.
|
|
|
|
|
|
|
|
See the example below.
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
``list_filter``
|
2005-07-17 12:20:57 +08:00
|
|
|
List of fields to filter by. Each field should either be a ``BooleanField``
|
2005-07-15 08:42:28 +08:00
|
|
|
or else a field with a ``ManyToOne`` relation.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
|
|
|
Here's an example of how ``list_display`` and ``list_filter`` work (taken
|
|
|
|
from the ``auth.user`` model)::
|
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff'),
|
|
|
|
list_filter = ('is_staff', 'is_superuser'),
|
2005-07-17 12:20:57 +08:00
|
|
|
|
|
|
|
This results in a admin that looks like:
|
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
.. image:: http://media.djangoproject.com/img/doc/users_changelist.png
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-15 08:42:28 +08:00
|
|
|
(This example also has ``search_fields`` defined; see below).
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
``ordering``
|
2005-07-16 04:37:03 +08:00
|
|
|
An ordering tuple (see the `Options for models`_, above) that gives a
|
2005-07-17 12:20:57 +08:00
|
|
|
different ordering for the admin change list. If this isn't given, the
|
|
|
|
model's default ordering will be used.
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
``save_as``
|
2005-07-16 04:37:03 +08:00
|
|
|
Enables a "save as" feature on object pages. Normally, objects have three
|
|
|
|
save options: "Save", "Save and continue editing", and "Save and add
|
2005-07-17 12:20:57 +08:00
|
|
|
another". If ``save_as`` is ``True``, "Save and add another" will be
|
2005-07-16 04:37:03 +08:00
|
|
|
replaced by a "Save as" button.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
|
|
|
"Save as" means the object will be saved as a new object (with a new ID),
|
|
|
|
rather than the old object.
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
``save_on_top``
|
2005-07-16 04:37:03 +08:00
|
|
|
If this option is ``True``, object pages will have the save buttons across
|
|
|
|
the top as well as at the bottom of the page.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
``search_fields``
|
2005-07-17 12:20:57 +08:00
|
|
|
A list of field names to provide a text search for. These fields should,
|
|
|
|
obviously, be some kind of text field, such as ``CharField`` or
|
|
|
|
``TextField``.
|
|
|
|
|
2005-07-16 04:37:03 +08:00
|
|
|
Model methods
|
|
|
|
=============
|
|
|
|
|
|
|
|
There are a number of methods you can define on model objects to control the
|
|
|
|
object's behavior. First, any methods you define will be available as methods
|
2005-07-17 12:20:57 +08:00
|
|
|
of object instances. For example::
|
2005-07-16 04:37:03 +08:00
|
|
|
|
|
|
|
class Pizza(meta.Model):
|
|
|
|
fields = (
|
|
|
|
...
|
2005-07-17 12:20:57 +08:00
|
|
|
)
|
|
|
|
|
2005-07-16 04:37:03 +08:00
|
|
|
def is_disgusting(self):
|
2005-07-17 12:20:57 +08:00
|
|
|
return "anchovies" in [topping.name for topping in self.get_topping_list()]
|
|
|
|
|
|
|
|
Now, every ``Pizza`` object will have a ``is_disgusting()`` method.
|
2005-07-16 04:37:03 +08:00
|
|
|
|
|
|
|
There are a few object methods that have special meaning:
|
|
|
|
|
|
|
|
``__repr__``
|
2005-07-17 12:20:57 +08:00
|
|
|
Django uses ``repr(obj)`` in a number of places, most notably as the value
|
|
|
|
inserted into a template when it displays an object. Thus, you should
|
2005-07-16 04:37:03 +08:00
|
|
|
return a nice, human-readable string for the object's ``__repr__``.
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-16 04:37:03 +08:00
|
|
|
``get_absolute_url``
|
2005-07-17 12:20:57 +08:00
|
|
|
If an object defines a ``get_absolute_url`` method, it is used to
|
|
|
|
associate a URL with an object. For example:
|
|
|
|
|
2005-07-16 04:37:03 +08:00
|
|
|
def get_absolute_url(self):
|
|
|
|
return "/pizzas/%i/" % self.id
|
2005-07-17 12:20:57 +08:00
|
|
|
|
|
|
|
The most useful place this is used is in the admin interface. If an object
|
|
|
|
defines ``get_absolute_url``, the object detail page will have a "View on
|
|
|
|
site" link that will jump you directly to the object's public view.
|
|
|
|
|
2005-07-16 04:37:03 +08:00
|
|
|
``_pre_save``
|
2005-07-17 12:20:57 +08:00
|
|
|
This method is called just before an object is saved to the database. For
|
|
|
|
example, you can use it to calculate aggregate values from other fields
|
|
|
|
before the object is saved.
|
|
|
|
|
2005-07-16 04:37:03 +08:00
|
|
|
``_post_save``
|
2005-07-17 12:20:57 +08:00
|
|
|
This method is called just after the object is saved to the database. This
|
|
|
|
could be used to update other tables, update cached information, etc.
|
|
|
|
|
2005-07-16 04:37:03 +08:00
|
|
|
Module-level methods
|
|
|
|
--------------------
|
|
|
|
|
2005-07-17 12:20:57 +08:00
|
|
|
Since each data class effectively turns into a "magic" Python module under
|
|
|
|
``django.models``, there are times you'll want to write methods that live in
|
|
|
|
that module. Any model method that begins with "_module_" is turned into a
|
|
|
|
module-level function::
|
2005-07-16 04:37:03 +08:00
|
|
|
|
|
|
|
class Pizza(meta.Model):
|
|
|
|
fields = (
|
|
|
|
...
|
|
|
|
)
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-16 04:37:03 +08:00
|
|
|
def _module_get_pizzas_to_deliver():
|
|
|
|
return get_list(delivered__exact=False)
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-16 04:37:03 +08:00
|
|
|
This will make the top-level ``pizzas`` module have a ``get_pizzas_to_deliver()``
|
|
|
|
method::
|
|
|
|
|
|
|
|
>>> from django.models.pizza_hut import pizzas
|
|
|
|
>>> pizzas.get_pizzas_to_deliver()
|
|
|
|
[ ... ]
|
2005-07-17 12:20:57 +08:00
|
|
|
|
|
|
|
Note that the scope of these methods is modified to be the same as the module
|
|
|
|
scope.
|
2005-07-16 04:37:03 +08:00
|
|
|
|
|
|
|
Manipulator methods
|
|
|
|
-------------------
|
|
|
|
|
|
|
|
Similarly, you can add methods to the object's manipulators (see the formfields
|
|
|
|
documentation for more on manipulators) by defining methods that being with
|
|
|
|
"_manipulator_". This is most useful for providing custom validators for certain
|
2005-07-17 12:20:57 +08:00
|
|
|
fields because manipulators automatically call any method that begins with
|
2005-07-16 04:37:03 +08:00
|
|
|
"validate"::
|
|
|
|
|
|
|
|
class Pizza(meta.Model):
|
|
|
|
fields = (
|
|
|
|
...
|
|
|
|
)
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-16 04:37:03 +08:00
|
|
|
def _manipulator_validate_customer_id(self, field_data, all_data):
|
|
|
|
from django.core import validators
|
|
|
|
from django.conf.settings import BAD_CUSTOMER_IDS
|
2005-07-17 12:20:57 +08:00
|
|
|
|
2005-07-16 04:37:03 +08:00
|
|
|
if int(field_data) in BAD_CUSTOMER_IDS:
|
|
|
|
raise validators.ValidationError("We don't deliver to this customer")
|