Opened 16 years ago
Closed 10 years ago
#7837 closed Bug (fixed)
Hierarchy in forms metaclasses can miss declarated fields in forms
Reported by: | Manuel Saelices | Owned by: | Manuel Saelices |
---|---|---|---|
Component: | Forms | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | yes |
Easy pickings: | no | UI/UX: | no |
Description
This was occurs after [7847]. Ok, [7847] is a good commit for make able to metaclass subclassing.
This is the scenary:
#### model class FooModel(models.Model): name = models.CharField(max_length=100) #### forms class GenericForm(forms.Form): extra_field = forms.CharField() class FooForm(GenericForm, forms.ModelForm): # to avoid python metaclass conflict error: __metaclass__ = type('FooFormMetaclass', (GenericForm.__metaclass__, forms.ModelForm.__metaclass__), {}) non_existing_field = forms.CharField() class Meta: model = FooModel
In previous scenario, FooForm
should contain both name
, extra_field
and nonexisting_field
. But only appears name
field in base_fields
.
See this test:
>>> from testapp.forms import FooForm >>> FooForm.base_fields.keys() ['name']
I will attach a patch thats fixes problem.
Attachments (6)
Change History (19)
by , 16 years ago
Attachment: | forms_metaclass_hierarchy_problem_fix.diff added |
---|
by , 16 years ago
Attachment: | forms_metaclass_hierarchy_problem_fix.2.diff added |
---|
It's only a Form naming change in tests
by , 16 years ago
Attachment: | forms_metaclass_hierarchy_problem_fix.3.diff added |
---|
Sorry... this is the good patch
by , 16 years ago
Attachment: | forms_metaclass_hierarchy_problem_fix.4.diff added |
---|
comment:1 by , 16 years ago
milestone: | → 1.0 beta |
---|
by , 16 years ago
Attachment: | forms_metaclass_hierarchy_problem_fix_r8049.diff added |
---|
Updated patch for [8049]
comment:2 by , 16 years ago
milestone: | 1.0 beta → 1.0 |
---|---|
Triage Stage: | Unreviewed → Accepted |
by , 16 years ago
Attachment: | forms_metaclass_hierarchy_problem_fix_r8335.diff added |
---|
Patch updated for [8335]
comment:3 by , 16 years ago
Status: | new → assigned |
---|
comment:4 by , 16 years ago
I'm frankly surprised that the business with __metaclass__
works at all -- I don't see why you want to subclass both Form
and ModelForm
, and I'm not entirely sure it ought to be possible at all.
In fact, you can just make GenericForm
a ModelForm
and then you don't have this weird metaclass hack.
comment:5 by , 16 years ago
A custom ModelForm
class that inherits another custom Form
was very useful in many ways. In my company there are several projects where we are using this patch, because is needed that functionality.
For example, you could use a ModelForm
with WTForm (one of the django snippets most rated), because you want having rapid and flexible layout design in your model forms.
comment:6 by , 16 years ago
I think multiple inneritance of ModelForm
and a normal Form
shouldn't be problematic. I don't like also use of __metaclass__ = ...
but this is only way to avoid python metaclass conflicts.
Could you find other way to achieve this?
comment:7 by , 16 years ago
milestone: | 1.0 → post-1.0 |
---|
I think this needs to wait for a proper fix -- if it's gonna work, it should work without the __metaclass__
hack. We can make that happen, but it'll be post-1.0.
comment:8 by , 16 years ago
There is a recipe of avoiding metaclass conflicts here: http://code.activestate.com/recipes/204197/
comment:10 by , 14 years ago
Severity: | → Normal |
---|---|
Type: | → Bug |
comment:11 by , 14 years ago
Easy pickings: | unset |
---|---|
Patch needs improvement: | set |
Patch needs improvement as per Jacob's comments.
comment:13 by , 10 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
Fixed in #19617.
Note that as documented the ModelForm
subclass should appear first in the MRO:
class FooForm(forms.ModelForm, GenericForm): pass
This patch fix problems