I'm using pycharm to debug the django development server for analysing a form instantiation where I'm using forms class inheritance.
If I run my application without stepping into any breakpoints (temporarily disabling all) then everything works as it should.
If I do stop at some breakpoint then the instantiated form produces different results.
In particular I have two multiple choices fields, and sometimes only one or both are not bound to the initial value.
I can see from the debugger that the part of the code that set field['field_name'].initial = value
is executed, nonetheless randomly I get field['field_name'].initial == None
.
My watches
panel is empty so there is no risk that I did modify any values by accident.
This is very weird for me! Why would a code execution change depending if I step or not into some breakpoints?
What might cause this odd behaviour?
EDIT:
After a bit more of inspection I limited the error to this block:
class MyForm(BaseForm):
period = forms.ChoiceField()
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['period'].choices = self.get_choices()
The method get_choices
return a dynamically computed list of choices, but even changing this with a static list doesn't change the result.
In fact whenever I perform an update and so I instantiate the MyForm
with data
and instance
arguments the following happens:
- If while debugging I do not step in the
self.fields['period'].choices = self.get_choices()
then everything works fine - If I do step in that line then the form does not validate, "Select a valid choice. XX is not one of the available choices."
EDIT 2:
In general this seems to happen anytime I step on a line that modifies an attribute of a field, that is, for example:
self.fields['period'].choices = self.get_choices()
self.fields['thingy'].initial = 'hello'
I'm so clueless..either pycharm is bugged or Django have some weird observer pattern that change a value B if the value A was accessed, even tough this seems unlikely..