tickets: 6473
This data as json
id | created | changetime | last_pulled_from_trac | stage | status | component | type | severity | version | resolution | summary | description | owner | reporter | keywords | easy | has_patch | needs_better_patch | needs_tests | needs_docs | ui_ux |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
6473 | 2008-01-28 02:38:23 | 2008-01-28 03:23:30 | 2022-03-06 03:37:14.435641 | Unreviewed | closed | Forms | dev | worksforme | Overriding ModelForm __init__ messes up instance | 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 '' >>> }}} | nobody | yourcelf | ModelForm __init__ instance | 0 | 0 | 0 | 0 | 0 | 0 |