Django

Code

Changeset 8974

Show
Ignore:
Timestamp:
09/06/08 11:51:24 (3 months ago)
Author:
adrian
Message:

Fixed #8883 -- Fixed some glitches in model reference. Thanks, arien

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/docs/topics/db/models.txt

    r8947 r8974  
    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. 
     
    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 
     
    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:: 
     
    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 tabl
    466 is to create instances of the intermediate model. 
     465The only way to create this type of relationship is to create instances of th
     466intermediate model. 
    467467 
    468468The ``remove`` method is disabled for similar reasons. However, the 
     
    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 
     
    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:: 
     
    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:: 
     
    627624 
    628625    from django.contrib.localflavor.us.models import USStateField 
     626 
    629627    class Person(models.Model): 
    630628        first_name = models.CharField(max_length=50) 
     
    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 
     
    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:: 
     
    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 
     
    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): 
     
    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 
     
    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