Skip to content

Commit

Permalink
Changed ModelForms to allow inheritance as long as their model attrib…
Browse files Browse the repository at this point in the history
…utes are the same.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@6917 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
jkocherhans committed Dec 13, 2007
1 parent e415eff commit 4c59ca6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
10 changes: 5 additions & 5 deletions django/newforms/models.py
Expand Up @@ -245,14 +245,14 @@ def __new__(cls, name, bases, attrs):

# 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)
Expand Down
8 changes: 7 additions & 1 deletion tests/modeltests/model_forms/models.py
Expand Up @@ -143,7 +143,7 @@ def __unicode__(self):
... 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:
Expand All @@ -155,6 +155,12 @@ def __unicode__(self):
...
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 #######################################################
Expand Down

0 comments on commit 4c59ca6

Please sign in to comment.