From 08fc16705fec15ab9b8b19333a07536aa8e4fe1d Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Thu, 4 May 2006 04:59:45 +0000 Subject: [PATCH] Proofread some of docs/model-api.txt. Still not done with this one. git-svn-id: http://code.djangoproject.com/svn/django/trunk@2829 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- docs/model-api.txt | 512 +++++++++++++++++++++++++++------------------ 1 file changed, 313 insertions(+), 199 deletions(-) diff --git a/docs/model-api.txt b/docs/model-api.txt index c24a3e76b9..dbe2c634d8 100644 --- a/docs/model-api.txt +++ b/docs/model-api.txt @@ -37,7 +37,7 @@ This example model defines a ``Person``, which has a ``first_name`` and ``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. -The above ``Person`` model would create an SQL table like this:: +The above ``Person`` model would create a database table like this:: CREATE TABLE myapp_person ( "id" serial NOT NULL PRIMARY KEY, @@ -45,7 +45,7 @@ The above ``Person`` model would create an SQL table like this:: "last_name" varchar(30) NOT NULL ); -Three technical notes: +Some technical notes: * The name of the table, ``myapp_person``, is automatically derived from some model metadata but can be overridden. See _`Table names` below. @@ -630,16 +630,16 @@ any other ``Field`` type: by including it as a class attribute of your model. ``ForeignKey`` requires a positional argument: The class to which the model is related. -For example, if a ``Place`` model is in a ``City`` -- that is, a ``City`` -contains multiple places but each ``Place`` is only in one ``City`` -- use the -following definitions:: +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:: - class City(models.Model): + class Manufacturer(models.Model): # ... - class Place(models.Model): + class Car(models.Model): + manufacturer = models.ForeignKey(Manufacturer) # ... - city = models.ForeignKey(City) To create a recursive relationship -- an object that has a many-to-one relationship with itself -- use ``models.ForeignKey('self')``. @@ -647,18 +647,30 @@ relationship with itself -- use ``models.ForeignKey('self')``. If you need to create a relationship on a model that has not yet been defined, you can use the name of the model, rather than the model object itself:: - class Place(models.Model): - # ... - city = models.ForeignKey("City") - - class City(models.Model): + class Car(models.Model): + manufacturer = models.ForeignKey('Manufacturer') # ... -The name of a ``ForeignKey`` (``city`` in the example above) generally should -be the name of the model, in singular form. Behind the scenes, Django appends -"_id" to the field name to create its database column name. However, your code + class Manufacturer(models.Model): + # ... + +Note, however, that support for strings around model names in ``ForeignKey`` is +quite new, and it can be buggy in some cases. + +Behind the scenes, Django appends ``"_id"`` to the field name to create its +database column name. In the above example, the database table for the ``Car`` +model will have a ``manufacturer_id`` column. (You can change this explicitly +by specifying ``db_column``; see ``db_column`` below.) However, your code should never have to deal with the database column name, unless you write -custom SQL. +custom SQL. You'll always deal with the field names of your model object. + +It's suggested, but not required, that the name of a ``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): + company_that_makes_it = models.ForeignKey(Manufacturer) + # ... See the `Many-to-one relationship model example`_ for a full example. @@ -684,7 +696,7 @@ relationship should work. All are optional: with ``models.LazyDate`` to limit choices of objects by date. For example:: - limit_choices_to = {'pub_date__lte' : models.LazyDate()} + limit_choices_to = {'pub_date__lte': models.LazyDate()} only allows the choice of related objects with a ``pub_date`` before the current date/time to be @@ -699,8 +711,9 @@ relationship should work. All are optional: that a user never enters more than 10 toppings. Note that this doesn't ensure more than 10 related - toppings ever get created. It just controls the - interface. + toppings ever get created. It simply controls the + admin interface; it doesn't enforce things at the + Python API level or database level. ``min_num_in_admin`` The minimum number of related objects displayed in the admin. Normally, at the creation stage, @@ -725,35 +738,9 @@ relationship should work. All are optional: Not used with ``edit_inline``. ``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:: - - models.ForeignKey(Pizza) - - the ``related_name`` will be "topping_set" (taken from - the class name), which will in turn give ``Pizza`` - a ``topping_set`` Object Set Descriptor. - - If you instead were to use:: - - models.ForeignKey(Pizza, related_name="munchies") - - then the Object Set Descriptor on ``Topping`` would - be called ``munchies``. - - 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 - ``primary_category`` and ``secondary_category`` - fields, to make sure that the ``Category`` objects - have the correct methods, you'd use fields like:: - - models.ForeignKey(Category, related_name="primary_stories") - models.ForeignKey(Category, related_name="secondary_stories") - - ...which would give ``Category`` objects two Object Set - descriptors - one called ``primary_stories`` and one - called ``secondary_stories``. + object back to this one. See the + `related objects documentation`_ for a full + explanation and example. ``to_field`` The field on the related object that the relation is to. By default, Django uses the primary key of @@ -761,6 +748,7 @@ relationship should work. All are optional: ======================= ============================================================ .. _`Database API reference`: http://www.djangoproject.com/documentation/db_api/ +.. _related objects documentation: http://www.djangoproject.com/documentation/db_api/#related-objects Many-to-many relationships ~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -784,11 +772,12 @@ here's how you'd represent that:: toppings = models.ManyToManyField(Topping) As with ``ForeignKey``, a relationship to self can be defined by using the -string ``"self"`` instead of the model name; references to as-yet undefined -models can be made by using a string containing the model name. +string ``'self'`` instead of the model name, and you can refer to as-yet +undefined models by using a string containing the model name. -The name of a ``ManyToManyField`` (``toppings`` in the example above) should -generally be a plural describing the set of related model objects. +It's suggested, but not required, that the name of a ``ManyToManyField`` +(``toppings`` in the example above) be a plural describing the set of related +model objects. Behind the scenes, Django creates an intermediary join table to represent the many-to-many relationship. @@ -797,11 +786,11 @@ It doesn't matter which model gets the ``ManyToManyField``, but you only need it in one of the models -- not in both. Generally, ``ManyToManyField`` instances should go in the object that's going -to be edited in the admin. In the above example, ``toppings`` is in ``Pizza`` -(rather than ``Topping`` having a ``pizzas`` ``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`` admin form would let -users select the toppings. +to be edited in the admin interface, if you're using Django's admin. In the +above example, ``toppings`` is in ``Pizza`` (rather than ``Topping`` having a +``pizzas`` ``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`` admin form would let users select the toppings. See the `Many-to-many relationship model example`_ for a full example. @@ -813,8 +802,7 @@ the relationship should work. All are optional: ======================= ============================================================ Argument Description ======================= ============================================================ - ``related_name`` See the description of ``related_name`` in - ``ForeignKey`` above. + ``related_name`` See the description under ``ForeignKey`` above. ``filter_interface`` Use a nifty unobtrusive Javascript "filter" interface instead of the usability-challenged ``