Opened 18 years ago
Closed 18 years ago
#6473 closed (worksforme)
Overriding ModelForm __init__ messes up instance
| Reported by: | Charlie DeTar | Owned by: | nobody |
|---|---|---|---|
| Component: | Forms | Version: | dev |
| Severity: | Keywords: | ModelForm __init__ instance | |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
Here's a model and two ModelForm classes for it:
from django import newforms as forms
from django.newforms import ModelForm
from django.db import models
class MyModel(models.Model):
my_field = models.CharField(max_length = 50)
class NormalModelForm(ModelForm):
class Meta:
model = MyModel
class OverridingModelForm(ModelForm):
def __init__(self, *args, **kwargs):
super(OverridingModelForm, self).__init__(args, kwargs)
class Meta:
model = MyModel
"NormalModelForm" behaves as expected. However, "OverridingModelForm" which overrides __init__ loses the data in its "instance", even though it calls super.__init__.
Test case:
>>> model = MyModel(my_field = 'happiness') >>> normal_form = NormalModelForm(instance = model) >>> normal_form.instance.my_field 'happiness' >>> overriding_form = OverridingModelForm(instance = model) >>> overriding_form.instance.my_field '' >>>
Note:
See TracTickets
for help on using tickets.
Your super call should be: