summaryrefslogtreecommitdiff
path: root/tests/modeltests
diff options
context:
space:
mode:
Diffstat (limited to 'tests/modeltests')
-rw-r--r--tests/modeltests/basic/models.py11
-rw-r--r--tests/modeltests/model_forms/models.py43
-rw-r--r--tests/modeltests/mutually_referential/models.py8
-rw-r--r--tests/modeltests/ordering/models.py4
4 files changed, 52 insertions, 14 deletions
diff --git a/tests/modeltests/basic/models.py b/tests/modeltests/basic/models.py
index 2f4b932fe0..557331a36e 100644
--- a/tests/modeltests/basic/models.py
+++ b/tests/modeltests/basic/models.py
@@ -5,6 +5,11 @@
This is a basic model with only two non-primary-key fields.
"""
+try:
+ set
+except NameError:
+ from sets import Set as set
+
from django.db import models
class Article(models.Model):
@@ -389,4 +394,10 @@ year, including Jan. 1 and Dec. 31.
>>> a.save()
>>> Article.objects.get(pk=a.id).headline
u'\u6797\u539f \u3081\u3050\u307f'
+
+# Model instances have a hash function, so they can be used in sets or as
+# dictionary keys. Two models compare as equal if their primary keys are equal.
+>>> s = set([a10, a11, a12])
+>>> Article.objects.get(headline='Article 11') in s
+True
"""
diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py
index f1fed8f1e1..c480899f84 100644
--- a/tests/modeltests/model_forms/models.py
+++ b/tests/modeltests/model_forms/models.py
@@ -64,11 +64,11 @@ class TextFile(models.Model):
def __unicode__(self):
return self.description
-
+
class ImageFile(models.Model):
description = models.CharField(max_length=20)
image = models.FileField(upload_to=tempfile.gettempdir())
-
+
def __unicode__(self):
return self.description
@@ -155,29 +155,52 @@ familiar with the mechanics.
... class Meta:
... model = Category
->>> class BadForm(CategoryForm):
+>>> class OddForm(CategoryForm):
... class Meta:
... model = Article
-Traceback (most recent call last):
-...
-ImproperlyConfigured: BadForm defines a different model than its parent.
+
+OddForm is now an Article-related thing, because BadForm.Meta overrides
+CategoryForm.Meta.
+>>> OddForm.base_fields.keys()
+['headline', 'slug', 'pub_date', 'writer', 'article', 'status', 'categories']
>>> class ArticleForm(ModelForm):
... class Meta:
... model = Article
+First class with a Meta class wins.
+
>>> class BadForm(ArticleForm, CategoryForm):
... pass
-Traceback (most recent call last):
-...
-ImproperlyConfigured: BadForm's base classes define more than one model.
+>>> OddForm.base_fields.keys()
+['headline', 'slug', 'pub_date', 'writer', 'article', 'status', 'categories']
-This one is OK since the subclass specifies the same model as the parent.
+Subclassing without specifying a Meta on the class will use the parent's Meta
+(or the first parent in the MRO if there are multiple parent classes).
+>>> class CategoryForm(ModelForm):
+... class Meta:
+... model = Category
>>> class SubCategoryForm(CategoryForm):
+... pass
+>>> SubCategoryForm.base_fields.keys()
+['name', 'slug', 'url']
+
+We can also subclass the Meta inner class to change the fields list.
+
+>>> class CategoryForm(ModelForm):
+... checkbox = forms.BooleanField()
+...
... class Meta:
... model = Category
+>>> class SubCategoryForm(CategoryForm):
+... class Meta(CategoryForm.Meta):
+... exclude = ['url']
+>>> print SubCategoryForm()
+<tr><th><label for="id_name">Name:</label></th><td><input id="id_name" type="text" name="name" maxlength="20" /></td></tr>
+<tr><th><label for="id_slug">Slug:</label></th><td><input id="id_slug" type="text" name="slug" maxlength="20" /></td></tr>
+<tr><th><label for="id_checkbox">Checkbox:</label></th><td><input type="checkbox" name="checkbox" id="id_checkbox" /></td></tr>
# Old form_for_x tests #######################################################
diff --git a/tests/modeltests/mutually_referential/models.py b/tests/modeltests/mutually_referential/models.py
index 7cf7bf8bb2..5176721f3d 100644
--- a/tests/modeltests/mutually_referential/models.py
+++ b/tests/modeltests/mutually_referential/models.py
@@ -1,18 +1,22 @@
"""
24. Mutually referential many-to-one relationships
-To define a many-to-one relationship, use ``ForeignKey()`` .
+Strings can be used instead of model literals to set up "lazy" relations.
"""
from django.db.models import *
class Parent(Model):
name = CharField(max_length=100, core=True)
+
+ # Use a simple string for forward declarations.
bestchild = ForeignKey("Child", null=True, related_name="favoured_by")
class Child(Model):
name = CharField(max_length=100)
- parent = ForeignKey(Parent)
+
+ # You can also explicitally specify the related app.
+ parent = ForeignKey("mutually_referential.Parent")
__test__ = {'API_TESTS':"""
# Create a Parent
diff --git a/tests/modeltests/ordering/models.py b/tests/modeltests/ordering/models.py
index 3e651d4ee7..9b342a9265 100644
--- a/tests/modeltests/ordering/models.py
+++ b/tests/modeltests/ordering/models.py
@@ -2,8 +2,8 @@
6. Specifying ordering
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 the
-results of ``get_list()`` and other similar functions.
+should be a list or tuple of field names. This tells Django how to order
+queryset results.
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.