From 22e016ff6c18a35a450519b94de63033f83e6b4f Mon Sep 17 00:00:00 2001 From: Justin Bronn Date: Thu, 28 Feb 2008 21:24:51 +0000 Subject: gis: Merged revisions 7105-7168 via svnmerge from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/gis@7176 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- tests/modeltests/model_forms/models.py | 43 ++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 10 deletions(-) (limited to 'tests/modeltests/model_forms/models.py') 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() + + + # Old form_for_x tests ####################################################### -- cgit v1.3