Fixed a couple typos in the modeltests' descriptions and made use of ReST inline literal markup for code snippets.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8325 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
c4d07d4d50
commit
1697f4e49f
|
@ -11,7 +11,7 @@ query the database.
|
||||||
|
|
||||||
If you need to use a table name for a many-to-many relationship that differs
|
If you need to use a table name for a many-to-many relationship that differs
|
||||||
from the default generated name, use the ``db_table`` parameter on the
|
from the default generated name, use the ``db_table`` parameter on the
|
||||||
ManyToMany field. This has no effect on the API for querying the database.
|
``ManyToMany`` field. This has no effect on the API for querying the database.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@ -87,7 +87,7 @@ Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
AttributeError: 'Author' object has no attribute 'last'
|
AttributeError: 'Author' object has no attribute 'last'
|
||||||
|
|
||||||
# Although the Article table uses a custom m2m table,
|
# Although the Article table uses a custom m2m table,
|
||||||
# nothing about using the m2m relationship has changed...
|
# nothing about using the m2m relationship has changed...
|
||||||
|
|
||||||
# Get all the authors for an article
|
# Get all the authors for an article
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
"""
|
"""
|
||||||
42. Storing files according to a custom storage system
|
42. Storing files according to a custom storage system
|
||||||
|
|
||||||
FileField and its variations can take a "storage" argument to specify how and
|
``FileField`` and its variations can take a ``storage`` argument to specify how
|
||||||
where files should be stored.
|
and where files should be stored.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import tempfile
|
import tempfile
|
||||||
|
|
|
@ -5,7 +5,7 @@ Fixtures are a way of loading data into the database in bulk. Fixure data
|
||||||
can be stored in any serializable format (including JSON and XML). Fixtures
|
can be stored in any serializable format (including JSON and XML). Fixtures
|
||||||
are identified by name, and are stored in either a directory named 'fixtures'
|
are identified by name, and are stored in either a directory named 'fixtures'
|
||||||
in the application directory, on in one of the directories named in the
|
in the application directory, on in one of the directories named in the
|
||||||
FIXTURE_DIRS setting.
|
``FIXTURE_DIRS`` setting.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
|
@ -2,8 +2,8 @@
|
||||||
34. Generic relations
|
34. Generic relations
|
||||||
|
|
||||||
Generic relations let an object have a foreign key to any object through a
|
Generic relations let an object have a foreign key to any object through a
|
||||||
content-type/object-id field. A generic foreign key can point to any object,
|
content-type/object-id field. A ``GenericForeignKey`` field can point to any
|
||||||
be it animal, vegetable, or mineral.
|
object, be it animal, vegetable, or mineral.
|
||||||
|
|
||||||
The canonical example is tags (although this example implementation is *far*
|
The canonical example is tags (although this example implementation is *far*
|
||||||
from complete).
|
from complete).
|
||||||
|
@ -32,7 +32,7 @@ class Comparison(models.Model):
|
||||||
A model that tests having multiple GenericForeignKeys
|
A model that tests having multiple GenericForeignKeys
|
||||||
"""
|
"""
|
||||||
comparative = models.CharField(max_length=50)
|
comparative = models.CharField(max_length=50)
|
||||||
|
|
||||||
content_type1 = models.ForeignKey(ContentType, related_name="comparative1_set")
|
content_type1 = models.ForeignKey(ContentType, related_name="comparative1_set")
|
||||||
object_id1 = models.PositiveIntegerField()
|
object_id1 = models.PositiveIntegerField()
|
||||||
|
|
||||||
|
@ -50,7 +50,7 @@ class Animal(models.Model):
|
||||||
latin_name = models.CharField(max_length=150)
|
latin_name = models.CharField(max_length=150)
|
||||||
|
|
||||||
tags = generic.GenericRelation(TaggedItem)
|
tags = generic.GenericRelation(TaggedItem)
|
||||||
comparisons = generic.GenericRelation(Comparison,
|
comparisons = generic.GenericRelation(Comparison,
|
||||||
object_id_field="object_id1",
|
object_id_field="object_id1",
|
||||||
content_type_field="content_type1")
|
content_type_field="content_type1")
|
||||||
|
|
||||||
|
|
|
@ -2,10 +2,10 @@
|
||||||
8. get_latest_by
|
8. get_latest_by
|
||||||
|
|
||||||
Models can have a ``get_latest_by`` attribute, which should be set to the name
|
Models can have a ``get_latest_by`` attribute, which should be set to the name
|
||||||
of a DateField or DateTimeField. If ``get_latest_by`` exists, the model's
|
of a ``DateField`` or ``DateTimeField``. If ``get_latest_by`` exists, the
|
||||||
manager will get a ``latest()`` method, which will return the latest object in
|
model's manager will get a ``latest()`` method, which will return the latest
|
||||||
the database according to that field. "Latest" means "having the date farthest
|
object in the database according to that field. "Latest" means "having the date
|
||||||
into the future."
|
farthest into the future."
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
"""
|
"""
|
||||||
35. DB-API Shortcuts
|
35. DB-API Shortcuts
|
||||||
|
|
||||||
get_object_or_404 is a shortcut function to be used in view functions for
|
``get_object_or_404()`` is a shortcut function to be used in view functions for
|
||||||
performing a get() lookup and raising a Http404 exception if a DoesNotExist
|
performing a ``get()`` lookup and raising a ``Http404`` exception if a
|
||||||
exception was raised during the get() call.
|
``DoesNotExist`` exception was raised during the ``get()`` call.
|
||||||
|
|
||||||
get_list_or_404 is a shortcut function to be used in view functions for
|
``get_list_or_404()`` is a shortcut function to be used in view functions for
|
||||||
performing a filter() lookup and raising a Http404 exception if a DoesNotExist
|
performing a ``filter()`` lookup and raising a ``Http404`` exception if a
|
||||||
exception was raised during the filter() call.
|
``DoesNotExist`` exception was raised during the ``filter()`` call.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
@ -16,7 +16,7 @@ from django.shortcuts import get_object_or_404, get_list_or_404
|
||||||
|
|
||||||
class Author(models.Model):
|
class Author(models.Model):
|
||||||
name = models.CharField(max_length=50)
|
name = models.CharField(max_length=50)
|
||||||
|
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ class Article(models.Model):
|
||||||
title = models.CharField(max_length=50)
|
title = models.CharField(max_length=50)
|
||||||
objects = models.Manager()
|
objects = models.Manager()
|
||||||
by_a_sir = ArticleManager()
|
by_a_sir = ArticleManager()
|
||||||
|
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
return self.title
|
return self.title
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
"""
|
"""
|
||||||
33. get_or_create()
|
33. get_or_create()
|
||||||
|
|
||||||
get_or_create() does what it says: it tries to look up an object with the given
|
``get_or_create()`` does what it says: it tries to look up an object with the
|
||||||
parameters. If an object isn't found, it creates one with the given parameters.
|
given parameters. If an object isn't found, it creates one with the given
|
||||||
|
parameters.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
|
@ -4,10 +4,10 @@
|
||||||
For many-to-many relationships that need extra fields on the intermediary
|
For many-to-many relationships that need extra fields on the intermediary
|
||||||
table, use an intermediary model.
|
table, use an intermediary model.
|
||||||
|
|
||||||
In this example, an ``Article`` can have multiple ``Reporter``s, and each
|
In this example, an ``Article`` can have multiple ``Reporter`` objects, and
|
||||||
``Article``-``Reporter`` combination (a ``Writer``) has a ``position`` field,
|
each ``Article``-``Reporter`` combination (a ``Writer``) has a ``position``
|
||||||
which specifies the ``Reporter``'s position for the given article (e.g. "Staff
|
field, which specifies the ``Reporter``'s position for the given article
|
||||||
writer").
|
(e.g. "Staff writer").
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
"""
|
"""
|
||||||
20. Multiple many-to-many relationships between the same two tables
|
20. Multiple many-to-many relationships between the same two tables
|
||||||
|
|
||||||
In this example, an Article can have many Categories (as "primary") and many
|
In this example, an ``Article`` can have many "primary" ``Category`` objects
|
||||||
Categories (as "secondary").
|
and many "secondary" ``Category`` objects.
|
||||||
|
|
||||||
Set ``related_name`` to designate what the reverse relationship is called.
|
Set ``related_name`` to designate what the reverse relationship is called.
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -1,15 +1,19 @@
|
||||||
"""
|
"""
|
||||||
28. Many-to-many relationships between the same two tables
|
28. Many-to-many relationships between the same two tables
|
||||||
|
|
||||||
In this example, A Person can have many friends, who are also people. Friendship is a
|
In this example, a ``Person`` can have many friends, who are also ``Person``
|
||||||
symmetrical relationship - if I am your friend, you are my friend.
|
objects. Friendship is a symmetrical relationship - if I am your friend, you
|
||||||
|
are my friend. Here, ``friends`` is an example of a symmetrical
|
||||||
|
``ManyToManyField``.
|
||||||
|
|
||||||
A person can also have many idols - but while I may idolize you, you may not think
|
A ``Person`` can also have many idols - but while I may idolize you, you may
|
||||||
the same of me. 'Idols' is an example of a non-symmetrical m2m field. Only recursive
|
not think the same of me. Here, ``idols`` is an example of a non-symmetrical
|
||||||
m2m fields may be non-symmetrical, and they are symmetrical by default.
|
``ManyToManyField``. Only recursive ``ManyToManyField`` fields may be
|
||||||
|
non-symmetrical, and they are symmetrical by default.
|
||||||
|
|
||||||
This test validates that the m2m table will create a mangled name for the m2m table if
|
This test validates that the many-to-many table is created using a mangled name
|
||||||
there will be a clash, and tests that symmetry is preserved where appropriate.
|
if there is a name clash, and tests that symmetry is preserved where
|
||||||
|
appropriate.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
"""
|
"""
|
||||||
27. Default manipulators
|
27. Default manipulators
|
||||||
|
|
||||||
Each model gets an AddManipulator and ChangeManipulator by default.
|
Each model gets an ``AddManipulator`` and ``ChangeManipulator`` by default.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
"""
|
"""
|
||||||
5. Many-to-many relationships
|
5. Many-to-many relationships
|
||||||
|
|
||||||
To define a many-to-many relationship, use ManyToManyField().
|
To define a many-to-many relationship, use ``ManyToManyField()``.
|
||||||
|
|
||||||
In this example, an article can be published in multiple publications,
|
In this example, an ``Article`` can be published in multiple ``Publication``
|
||||||
and a publication has multiple articles.
|
objects, and a ``Publication`` has multiple ``Article`` objects.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
"""
|
"""
|
||||||
4. Many-to-one relationships
|
4. Many-to-one relationships
|
||||||
|
|
||||||
To define a many-to-one relationship, use ``ForeignKey()`` .
|
To define a many-to-one relationship, use ``ForeignKey()``.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
"""
|
"""
|
||||||
XX. Generating HTML forms from models
|
XX. Generating HTML forms from models
|
||||||
|
|
||||||
This is mostly just a reworking of the form_for_model/form_for_instance tests
|
This is mostly just a reworking of the ``form_for_model``/``form_for_instance``
|
||||||
to use ModelForm. As such, the text may not make sense in all cases, and the
|
tests to use ``ModelForm``. As such, the text may not make sense in all cases,
|
||||||
examples are probably a poor fit for the ModelForm syntax. In other words,
|
and the examples are probably a poor fit for the ``ModelForm`` syntax. In other
|
||||||
most of these tests should be rewritten.
|
words, most of these tests should be rewritten.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
"""
|
"""
|
||||||
19. OR lookups
|
19. OR lookups
|
||||||
|
|
||||||
To perform an OR lookup, or a lookup that combines ANDs and ORs,
|
To perform an OR lookup, or a lookup that combines ANDs and ORs, combine
|
||||||
combine QuerySet objects using & and | operators.
|
``QuerySet`` objects using ``&`` and ``|`` operators.
|
||||||
|
|
||||||
Alternatively, use positional arguments, and pass one or more expressions of
|
Alternatively, use positional arguments, and pass one or more expressions of
|
||||||
clauses using the variable ``django.db.models.Q`` (or any object with an
|
clauses using the variable ``django.db.models.Q`` (or any object with an
|
||||||
add_to_query method).
|
``add_to_query`` method).
|
||||||
"""
|
"""
|
||||||
# Python 2.3 doesn't have sorted()
|
# Python 2.3 doesn't have sorted()
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
Specify default ordering for a model using the ``ordering`` attribute, which
|
Specify default ordering for a model using the ``ordering`` attribute, which
|
||||||
should be a list or tuple of field names. This tells Django how to order
|
should be a list or tuple of field names. This tells Django how to order
|
||||||
queryset results.
|
``QuerySet`` results.
|
||||||
|
|
||||||
If a field name in ``ordering`` starts with a hyphen, that field will be
|
If a field name in ``ordering`` starts with a hyphen, that field will be
|
||||||
ordered in descending order. Otherwise, it'll be ordered in ascending order.
|
ordered in descending order. Otherwise, it'll be ordered in ascending order.
|
||||||
|
|
|
@ -2,8 +2,8 @@
|
||||||
"""
|
"""
|
||||||
42. Serialization
|
42. Serialization
|
||||||
|
|
||||||
``django.core.serializers`` provides interfaces to converting Django querysets
|
``django.core.serializers`` provides interfaces to converting Django
|
||||||
to and from "flat" data (i.e. strings).
|
``QuerySet`` objects to and from "flat" data (i.e. strings).
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
|
@ -11,10 +11,10 @@ The server Response objects are annotated with the details
|
||||||
of the contexts and templates that were rendered during the
|
of the contexts and templates that were rendered during the
|
||||||
process of serving the request.
|
process of serving the request.
|
||||||
|
|
||||||
Client objects are stateful - they will retain cookie (and
|
``Client`` objects are stateful - they will retain cookie (and
|
||||||
thus session) details for the lifetime of the Client instance.
|
thus session) details for the lifetime of the ``Client`` instance.
|
||||||
|
|
||||||
This is not intended as a replacement for Twill,Selenium, or
|
This is not intended as a replacement for Twill, Selenium, or
|
||||||
other browser automation frameworks - it is here to allow
|
other browser automation frameworks - it is here to allow
|
||||||
testing against the contexts and templates produced by a view,
|
testing against the contexts and templates produced by a view,
|
||||||
rather than the HTML rendered to the end-user.
|
rather than the HTML rendered to the end-user.
|
||||||
|
|
|
@ -1,16 +1,16 @@
|
||||||
"""
|
"""
|
||||||
38. User-registered management commands
|
38. User-registered management commands
|
||||||
|
|
||||||
The manage.py utility provides a number of useful commands for managing a
|
The ``manage.py`` utility provides a number of useful commands for managing a
|
||||||
Django project. If you want to add a utility command of your own, you can.
|
Django project. If you want to add a utility command of your own, you can.
|
||||||
|
|
||||||
The user-defined command 'dance' is defined in the management/commands
|
The user-defined command ``dance`` is defined in the management/commands
|
||||||
subdirectory of this test application. It is a simple command that responds
|
subdirectory of this test application. It is a simple command that responds
|
||||||
with a printed message when invoked.
|
with a printed message when invoked.
|
||||||
|
|
||||||
For more details on how to define your own manage.py commands, look at the
|
For more details on how to define your own ``manage.py`` commands, look at the
|
||||||
django.core.management.commands directory. This directory contains the
|
``django.core.management.commands`` directory. This directory contains the
|
||||||
definitions for the base Django manage.py commands.
|
definitions for the base Django ``manage.py`` commands.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__test__ = {'API_TESTS': """
|
__test__ = {'API_TESTS': """
|
||||||
|
|
Loading…
Reference in New Issue