Django(trunk) and class based generic views: one form's initial data appearing in another one's

前端 未结 2 337
难免孤独
难免孤独 2021-01-28 02:45

I\'ve run into a strange problem where data seems to persist accross different views and requests until the server gets restarted.

I\'ve managed to reduce the issue to t

相关标签:
2条回答
  • 2021-01-28 03:13

    Because you're mutating the kwargs that are passed in, which come from class-level properties in the view class.

    Instead, copy them and update the copy:

    initial_defaults = {'bug': 'no'}
    initial_defaults.update(kwargs.get('initial', {}))
    defaults = kwargs.copy()
    defaults['initial'] = initial_defaults 
    
    0 讨论(0)
  • 2021-01-28 03:16

    You might want to specify Django-1.3 development, generic class view doesn't exist in Django 1.2.5. In your forms.py file, can you comment the following lines and try again:

    class UpdateForm(forms.ModelForm):
        class Meta:
            model = Foo
    
        def __init__(self, *args, **kwargs):
            #kwargs.setdefault('initial', {})
            #kwargs['initial'].update({'bug': 'WHY??'})
            super(UpdateForm, self).__init__(*args, **kwargs)
    
    0 讨论(0)
提交回复
热议问题