1

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..

Leonardo
  • 4,046
  • 5
  • 44
  • 85
  • how do you run the debug? and the server? are the python interpreter, virtualenv and environment vars the same in both cases? – Eduard Gamonal Mar 16 '18 at 11:39
  • I do not change anything in the configuration, in one case I just click on the 'disable all breakpoints' and everything works. By simply enabling the breakpoints creates the problem as described – Leonardo Mar 16 '18 at 11:42
  • any updates about this problem? Have the same issue.. – KravAn Sep 05 '18 at 20:30
  • In my case I think one of the possible reasons could be related to having everything in debug mode running twice. So most probably I inspect the first or the second request incoming. That's another mystery by itself that I couldn't solve yet. See my question here https://stackoverflow.com/q/51131988/1191416 – Leonardo Sep 06 '18 at 09:18

0 Answers0