From 37962ecea79cb9d296645a5324cd91818fed65b3 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Thu, 14 Feb 2008 12:56:49 +0000 Subject: Fixed #6337. Refs #3632 -- Fixed ModelForms subclassing, to the extent that it can be made to work. This ended up being an almost complete rewrite of ModelForms.__new__, but should be backwards compatible (although the text of one error message has changed, which is only user visible and only if you pass in invalid code). Documentation updated, also. This started out as a patch from semenov (many thanks!), but by the time all the problems were hammered out, little of the original was left. Still, it was a good starting point. git-svn-id: http://code.djangoproject.com/svn/django/trunk@7112 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- tests/modeltests/model_forms/models.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py index f1fed8f1e1..0d429b2a71 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 @@ -160,7 +160,7 @@ familiar with the mechanics. ... model = Article Traceback (most recent call last): ... -ImproperlyConfigured: BadForm defines a different model than its parent. +ImproperlyConfigured: BadForm's base classes define more than one model. >>> class ArticleForm(ModelForm): ... class Meta: @@ -179,6 +179,20 @@ This one is OK since the subclass specifies the same model as the parent. ... model = Category +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 +... exclude = ['url'] +>>> class SubCategoryForm(CategoryForm): +... pass + +>>> print SubCategoryForm() + + + # Old form_for_x tests ####################################################### >>> from django.newforms import ModelForm, CharField -- cgit v1.3