summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph Kocherhans <joseph@jkocherhans.com>2007-12-13 03:14:31 +0000
committerJoseph Kocherhans <joseph@jkocherhans.com>2007-12-13 03:14:31 +0000
commit4c59ca602063f5de88595dfb18b0f83bc7bba0f7 (patch)
treed532502bf85d517700e7153be27410117b9c04fb
parente415eff0ead50882525129b0d5fd8ca95f1d30a7 (diff)
Changed ModelForms to allow inheritance as long as their model attributes are the same.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@6917 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/newforms/models.py10
-rw-r--r--tests/modeltests/model_forms/models.py8
2 files changed, 12 insertions, 6 deletions
diff --git a/django/newforms/models.py b/django/newforms/models.py
index 8d6ee1fda2..17fd1cf2e2 100644
--- a/django/newforms/models.py
+++ b/django/newforms/models.py
@@ -245,14 +245,14 @@ class ModelFormMetaclass(type):
# If a model is defined, extract form fields from it and add them to base_fields
if attrs['_meta'].model is not None:
- # Don't allow a subclass to define a Meta model if a parent class has.
- # Technically the right fields would be generated, but the save
- # method will not deal with more than one model.
+ # Don't allow a subclass to define a different Meta model than a
+ # parent class has. Technically the right fields would be generated,
+ # but the save method will not deal with more than one model.
for base in bases:
base_opts = getattr(base, '_meta', None)
base_model = getattr(base_opts, 'model', None)
- if base_model is not None:
- raise ImproperlyConfigured('%s defines more than one model.' % name)
+ if base_model and base_model is not opts.model:
+ raise ImproperlyConfigured('%s defines a different model than its parent.' % name)
model_fields = fields_for_model(opts.model, opts.fields, opts.exclude)
# fields declared in base classes override fields from the model
model_fields.update(declared_fields)
diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py
index 5e9584496e..17c3b3551c 100644
--- a/tests/modeltests/model_forms/models.py
+++ b/tests/modeltests/model_forms/models.py
@@ -143,7 +143,7 @@ familiar with the mechanics.
... model = Article
Traceback (most recent call last):
...
-ImproperlyConfigured: BadForm defines more than one model.
+ImproperlyConfigured: BadForm defines a different model than its parent.
>>> class ArticleForm(ModelForm):
... class Meta:
@@ -155,6 +155,12 @@ Traceback (most recent call last):
...
ImproperlyConfigured: BadForm's base classes define more than one model.
+This one is OK since the subclass specifies the same model as the parent.
+
+>>> class SubCategoryForm(CategoryForm):
+... class Meta:
+... model = Category
+
# Old form_for_x tests #######################################################