From ccc46a0f2fa14eb34f18aae57f71251180cdd548 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Mon, 17 Apr 2006 12:47:49 +0000 Subject: [PATCH] magic-removal: Updated model API documentation. git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2708 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- docs/model-api.txt | 392 ++++++++++++++++++++++++++------------------- 1 file changed, 228 insertions(+), 164 deletions(-) diff --git a/docs/model-api.txt b/docs/model-api.txt index 1a67e6181b..a22d9f9fad 100644 --- a/docs/model-api.txt +++ b/docs/model-api.txt @@ -8,9 +8,10 @@ model maps to a single database table. The basics: - * Each model is a Python class that subclasses ``django.core.meta.Model``. + * Each model is a Python class that subclasses ``django.db.models.Model``. * Each attribute of the model represents a database field. * Model metadata (non-field information) goes in an inner class named ``Meta``. + * Metadata used for administration purposes goes into an inner class named ``Admin``. A companion to this document is the `official repository of model examples`_. @@ -20,15 +21,13 @@ Field objects ============= The most important part of a model is the list of database fields it defines. -Fields are defined by class attributes. Each class attribute in a model, aside -from the optional inner ``class Meta``, should be an instance of a -``meta.Field`` subclass. +Fields are defined by class attributes. In this example, there are two fields, ``first_name`` and ``last_name`` :: - class Person(meta.Model): - first_name = meta.CharField(maxlength=30) - last_name = meta.CharField(maxlength=30) + class Person(models.Model): + first_name = models.CharField(maxlength=30) + last_name = models.CharField(maxlength=30) Django will use ``first_name`` and ``last_name`` as the database column names. @@ -40,19 +39,19 @@ name, converting underscores to spaces. In this example, the human-readable name is ``"Person's first name"``:: - first_name = meta.CharField("Person's first name", maxlength=30) + first_name = models.CharField("Person's first name", maxlength=30) In this example, the human-readable name is ``"first name"``:: - first_name = meta.CharField(maxlength=30) + first_name = models.CharField(maxlength=30) ``ForeignKey``, ``ManyToManyField`` and ``OneToOneField`` require the first argument to be a model class, so use the ``verbose_name`` keyword argument to specify the human-readable name:: - poll = meta.ForeignKey(Poll, verbose_name="the related poll") - sites = meta.ManyToManyField(Site, verbose_name="list of sites") - place = meta.OneToOneField(Place, verbose_name="related place") + poll = models.ForeignKey(Poll, verbose_name="the related poll") + sites = models.ManyToManyField(Site, verbose_name="list of sites") + place = models.OneToOneField(Place, verbose_name="related place") Convention is not to capitalize the first letter of the ``verbose_name``. Django will automatically capitalize the first letter where it needs to. @@ -104,24 +103,23 @@ The following arguments are available to all field types. All are optional. The first element in each tuple is the actual value to be stored. The second element is the human-readable name for the option. - Define the choices list **outside** of your model class, not inside it. - For example, this is not valid:: + The choices list can be defined either as part of your model class:: - class Foo(meta.Model): + class Foo(models.Model): GENDER_CHOICES = ( ('M', 'Male'), ('F', 'Female'), ) - gender = meta.CharField(maxlength=1, choices=GENDER_CHOICES) + gender = models.CharField(maxlength=1, choices=GENDER_CHOICES) - But this is valid:: + or outside your model class altogether:: GENDER_CHOICES = ( ('M', 'Male'), ('F', 'Female'), ) - class Foo(meta.Model): - gender = meta.CharField(maxlength=1, choices=GENDER_CHOICES) + class Foo(models.Model): + gender = models.CharField(maxlength=1, choices=GENDER_CHOICES) ``core`` For objects that are edited inline to a related object. @@ -162,7 +160,7 @@ The following arguments are available to all field types. All are optional. If you don't specify ``primary_key=True`` for any fields in your model, Django will automatically add this field:: - id = meta.AutoField('ID', primary_key=True) + id = models.AutoField('ID', primary_key=True) Thus, you don't need to set ``primary_key=True`` on any of your fields unless you want to override the default primary-key behavior. @@ -359,12 +357,12 @@ Here are all available field types: For example, to store numbers up to 999 with a resolution of 2 decimal places, you'd use:: - meta.FloatField(..., max_digits=5, decimal_places=2) + models.FloatField(..., max_digits=5, decimal_places=2) And to store numbers up to approximately one billion with a resolution of 10 decimal places:: - meta.FloatField(..., max_digits=19, decimal_places=10) + models.FloatField(..., max_digits=19, decimal_places=10) The admin represents this as an ```` (a single-line input). @@ -420,7 +418,7 @@ Here are all available field types: from which to auto-populate the slug, via JavaScript, in the object's admin form:: - meta.SlugField(prepopulate_from=("pre_name", "name")) + models.SlugField(prepopulate_from=("pre_name", "name")) ``prepopulate_from`` doesn't accept DateTimeFields. @@ -466,7 +464,7 @@ Relationships ------------- Clearly, the power of relational databases lies in relating tables to each -other. Django offers ways to define the most common types of database +other. Django offers ways to define the three most common types of database relationships: Many-to-one, many-to-many and one-to-one. Many-to-one relationships @@ -479,23 +477,34 @@ any other ``Field`` type: by including it as a class attribute of your model. 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`` -- here's -how you'd represent that:: +contains multiple places but each ``Place`` is only in one ``City`` -- use the +following definitions:: - class City(meta.Model): + class City(models.Model): # ... - class Place(meta.Model): + class Place(models.Model): # ... - city = meta.ForeignKey(City) + city = models.ForeignKey(City) To create a recursive relationship -- an object that has a many-to-one -relationship with itself -- use ``meta.ForeignKey("self")``. +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): + # ... The name of a ``ForeignKey`` (``city`` in the example above) generally should -be the name of the model, singular. Behind the scenes, Django appends "_id" to -the field name to create its database column name. But your code should never -have to deal with the database column name, unless you write custom SQL. +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 +should never have to deal with the database column name, unless you write +custom SQL. See the `Many-to-one relationship model example`_ for a full example. @@ -510,7 +519,7 @@ relationship should work. All are optional: ``edit_inline`` If not ``False``, this related object is edited "inline" on the related object's page. This means that the object will not have its own admin - interface. Use either ``meta.TABULAR`` or ``meta.STACKED``, + interface. Use either ``models.TABULAR`` or ``models.STACKED``, which, respectively, designate whether the inline-editable objects are displayed as a table or as a "stack" of fieldsets. @@ -518,10 +527,10 @@ relationship should work. All are optional: ``limit_choices_to`` A dictionary of lookup arguments and values (see the `Database API reference`_) that limit the available admin choices for this object. Use this - with ``meta.LazyDate`` to limit choices of objects + with ``models.LazyDate`` to limit choices of objects by date. For example:: - limit_choices_to = {'pub_date__lte' : meta.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 @@ -565,20 +574,18 @@ relationship should work. All are optional: object back to this one. For example, when if ``Topping`` has this field:: - meta.ForeignKey(Pizza) + models.ForeignKey(Pizza) - the ``related_name`` will be "topping" (taken from + the ``related_name`` will be "topping_set" (taken from the class name), which will in turn give ``Pizza`` - the methods ``get_topping_list()`` and - ``get_topping_count()``. + a ``topping_set`` Object Set Descriptor. If you instead were to use:: - meta.ForeignKey(Pizza, related_name="munchie") + models.ForeignKey(Pizza, related_name="munchies") - then the methods would be called - ``get_munchie_list()``, ``get_munchie_count()``, - etc. + 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 @@ -587,12 +594,12 @@ relationship should work. All are optional: fields, to make sure that the ``Category`` objects have the correct methods, you'd use fields like:: - meta.ForeignKey(Category, related_name="primary_story") - meta.ForeignKey(Category, related_name="secondary_story") + models.ForeignKey(Category, related_name="primary_stories") + models.ForeignKey(Category, related_name="secondary_stories") - ...which would give the ``Category`` objects - methods named ``get_primary_story_list()`` and - ``get_secondary_story_list()``. + ...which would give ``Category`` objects two Object Set + descriptors - one called ``primary_stories`` and one + called ``secondary_stories``. ``to_field`` The field on the related object that the relation is to. By default, Django uses the primary key of @@ -615,15 +622,19 @@ 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:: - class Topping(meta.Model): + class Topping(models.Model): # ... - class Pizza(meta.Model): + class Pizza(models.Model): # ... - toppings = meta.ManyToManyField(Topping) + toppings = models.ManyToManyField(Topping) -The name of a ``ManyToManyField`` (``toppings`` in the example above) generally -should be the name of the model, plural. +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. + +The name of a ``ManyToManyField`` (``toppings`` in the example above) should +generally 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. @@ -654,7 +665,7 @@ the relationship should work. All are optional: ``filter_interface`` Use a nifty unobtrusive Javascript "filter" interface instead of the usability-challenged ``