Ticket #8883: topics-db-models.diff

File topics-db-models.diff, 7.6 KB (added by arien, 16 years ago)

typo and markup fixes, including some minor editing of the text

  • docs/topics/db/models.txt

     
    342342object that's going to be edited in the admin interface, if you're using
    343343Django's admin. In the above example, ``toppings`` is in ``Pizza`` (rather than
    344344``Topping`` having a ``pizzas`` :class:`~django.db.models.ManyToManyField` )
    345 because it's more natural to think about a ``Pizza`` having toppings than a
     345because it's more natural to think about a pizza having toppings than a
    346346topping being on multiple pizzas. The way it's set up above, the ``Pizza`` admin
    347347form would let users select the toppings.
    348348
     
    407407There are a few restrictions on the intermediate model:
    408408
    409409    * Your intermediate model must contain one - and *only* one - foreign key
    410       on the target model (this would be ``Person`` in our example). If you
     410      to the target model (this would be ``Person`` in our example). If you
    411411      have more than one foreign key, a validation error will be raised.
    412412 
    413413    * Your intermediate model must contain one - and *only* one - foreign key
    414       on the source model (this would be ``Group`` in our example). If you
     414      to the source model (this would be ``Group`` in our example). If you
    415415      have more than one foreign key, a validation error will be raised.
    416416
    417417    * The only exception to this is a model which has a many-to-many
     
    426426      :ref:`the model field reference <manytomany-arguments>`).
    427427
    428428Now that you have set up your :class:`~django.db.models.ManyToManyField` to use
    429 your intermediary model (Membership, in this case), you're ready to start
     429your intermediary model (``Membership``, in this case), you're ready to start
    430430creating some many-to-many relationships. You do this by creating instances of
    431431the intermediate model::
    432432   
     
    457457    # AND NEITHER WILL THIS
    458458    >>> beatles.members = [john, paul, ringo, george]
    459459   
    460 Why? You can't just create a relationship between a Person and a Group - you
    461 need to specify all the detail for the relationship required by the
    462 Membership table. The simple ``add``, ``create`` and assignment calls
     460Why? You can't just create a relationship between a ``Person`` and a ``Group``
     461- you need to specify all the detail for the relationship required by the
     462``Membership`` model. The simple ``add``, ``create`` and assignment calls
    463463don't provide a way to specify this extra detail. As a result, they are
    464464disabled for many-to-many relationships that use an intermediate model.
    465 The only way to create a many-to-many relationship with an intermediate table
    466 is to create instances of the intermediate model.
     465The only way to create this type of relationship is to create instances of the
     466intermediate model.
    467467
    468468The ``remove`` method is disabled for similar reasons. However, the
    469469``clear()`` method can be used to remove all many-to-many relationships
     
    481481    >>> Groups.objects.filter(person__name__startswith='Paul')
    482482    [<Group: The Beatles>]
    483483
    484 As you are using an intermediate table, you can also query on the attributes
    485 of the intermediate model::
     484As you are using an intermediate model, you can also query on its attributes::
    486485
    487486    # Find all the members of the Beatles that joined after 1 Jan 1961
    488487    >>> Person.objects.filter(
     
    518517:ref:`recursive relationship <recursive-relationships>`
    519518can be defined and
    520519:ref:`references to as-yet undefined models <lazy-relationships>`
    521 can be made; see
    522 :class:`the model field reference <django.db.models.fields.OneToOneField>`
    523 for details.
     520can be made; see :ref:`the model field reference <ref-onetoone>` for details.
    524521
    525522.. seealso::
    526523
     
    542539Models across files
    543540-------------------
    544541
    545 It's perfectly OK to relate a model to one from another app. To do this, just
     542It's perfectly OK to relate a model to one from another app. To do this,
    546543import the related model at the top of the model that holds your model. Then,
    547544just refer to the other model class wherever needed. For example::
    548545
     
    626623For example, this model has a few custom methods::
    627624
    628625    from django.contrib.localflavor.us.models import USStateField
     626
    629627    class Person(models.Model):
    630628        first_name = models.CharField(max_length=50)
    631629        last_name = models.CharField(max_length=50)
     
    741739        row = cursor.fetchone()
    742740        return row
    743741
    744 :class:`connection <django.db.backends.DatabaseWrapper>` and
    745 :class:`<django.db.backends.CursorWrapper>` mostly implement the standard Python
     742:class:`connection <django.db.backends.DatabaseWrapper>` and :class:`cursor
     743<django.db.backends.CursorWrapper>` mostly implement the standard Python
    746744DB-API -- see :pep:`249` -- with the addition of Django's :ref:`transaction
    747745handling <topics-db-transactions>`. If you're not familiar with the Python
    748746DB-API, note that the SQL statement in :meth:`cursor.execute()
     
    818816~~~~~~~~~~~~~~~~~~~~
    819817
    820818When an abstract base class is created, Django makes any :ref:`Meta <meta-options>`
    821 inner class you declared on the base class available as an
    822 attribute. If a child class does not declared its own :ref:`Meta <meta-options>`
     819inner class you declared in the base class available as an
     820attribute. If a child class does not declare its own :ref:`Meta <meta-options>`
    823821class, it will inherit the parent's :ref:`Meta <meta-options>`. If the child wants to
    824822extend the parent's :ref:`Meta <meta-options>` class, it can subclass it. For example::
    825823
     
    896894
    897895The second type of model inheritance supported by Django is when each model in
    898896the hierarchy is a model all by itself. Each model corresponds to its own
    899 database table and can be queried and created indvidually. The inheritance
     897database table and can be queried and created individually. The inheritance
    900898relationship introduces links between the child model and each of its parents
    901 (via an automatically-created :class`~django.db.models.fields.OneToOneField`).
     899(via an automatically-created :class:`~django.db.models.fields.OneToOneField`).
    902900For example::
    903901
    904902    class Place(models.Model):
     
    945943:attr:`django.db.models.Options.get_latest_by` attribute, it will inherit these from its parent.
    946944
    947945If the parent has an ordering and you don't want the child to have any natural
    948 ordering, you can explicity set it to be empty::
     946ordering, you can explicitly disable it::
    949947
    950948    class ChildModel(ParentModel):
    951949        ...
     
    974972
    975973    class Supplier(Place):
    976974        # Must specify related_name on all relations.
    977         customers = models.ManyToManyField(Restaurant,
    978                 related_name='provider')
     975        customers = models.ManyToManyField(Restaurant, related_name='provider')
    979976
    980977
    981978Specifying the parent link field
     
    994991
    995992Just as with Python's subclassing, it's possible for a Django model to inherit
    996993from multiple parent models. Keep in mind that normal Python name resolution
    997 rules apply. The first base class that a particular name appears in (e.g.
    998 :ref:`Meta <meta-options>`) will be the one that is used; for example,
    999 his means that if multiple parents contain a :ref:`Meta <meta-options>` class, only
    1000 the first one is going to be used, and all others will be ignored.
     994rules apply. The first base class that a particular name (e.g. :ref:`Meta
     995<meta-options>`) appears in will be the one that is used; for example, this
     996means that if multiple parents contain a :ref:`Meta <meta-options>` class,
     997only the first one is going to be used, and all others will be ignored.
    1001998
    1002999Generally, you won't need to inherit from multiple parents. The main use-case
    10031000where this is useful is for "mix-in" classes: adding a particular extra
Back to Top