1
0
mirror of https://github.com/django/django.git synced 2025-10-24 14:16:09 +00:00

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
This commit is contained in:
Malcolm Tredinnick
2008-02-14 12:56:49 +00:00
parent 5078010a31
commit 37962ecea7
4 changed files with 112 additions and 55 deletions

View File

@@ -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()
<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>
# Old form_for_x tests #######################################################
>>> from django.newforms import ModelForm, CharField