Debug a validation form in Pycharm

I am trying to debug form validation in Django using Pycharm. My form validation failed while uploading the image via ImageField, and I want to find out why it doesn’t work. However, whenever I try to debug the validation process and enter into and through the initialization of the form with POST data, it does not even try to validate, but in the end it causes an error due to empty fields when trying to save the form data to the database. It drives me crazy ... how can behavior change depending on whether I observe individual steps or not? I also tried setting some breakpoints, for example. during the completely pure BaseForm class method, but it seems to never get there.

Edit: Here is the code

My model and form:

class Car(models.Model): ... image = models.ImageField(upload_to='car_images/',blank=True,null=True) class CarForm(ModelForm): class Meta: model = Car 

My view:

 def create_car(request): if request.method == 'POST': car_form = CarForm(request.POST,request.FILES) if car_form.is_valid(): ... 
+4
source share
1 answer

This is a real pain. I have this problem twice, and I have not yet found a good solution.

This is an example of safe debugging when overriding the __init__ form method.

 class MyForm(forms.ModelForm): def __init__(self, *args, **kwargs): super(MyForm, self).__init__(*args, **kwargs) # Disable form validation for debugging purposes unless the # object is fully initialized. Set breakpoints BELOW this line. del self._errors # Write some additional initializations here, debug safely. ... # Initialization is finished. Enable form validation. self._errors = None 

If you want to debug the base classes of classes, then patch the Django code in the same way.

You can leave or remove this additional code after debugging - this is not significant. But it is better to leave this business in the future.

So what is really going on here?

(bugreport on the Django project website: https://code.djangoproject.com/ticket/24710 )

The problem seems to be that the gango BaseForm.errors getter property (which is actually a method with @property decorator) does too much. It calls the full_clean() method, which changes the value of the _errors property, so that the errors getter does not do the same job again on repeated calls.

 class BaseForm(object): @property def errors(self): if self._errors is None: self.full_clean() return self._errors def full_clean(self): self._errors = ErrorDict() ... 

Of course, the PyCharm debugger assumes that properties are just properties, and they do not make critical changes to the internal state of objects. Thus, the debugger calls errors getter to show its value in the Variables window when debugging the __ini__ method. And that breaks the normal flow of execution.

(That's why you should define methods like get_error() in such cases, not properties)

One possible suggestion might be to avoid breakpoints and phased execution inside the __init__ form method. But if you really need to debug it, change the code so that the _errors property _errors not exist during step-by-step execution. This will prevent calls to the full_clean method. The PyCharm debugger will receive an error each time it tries to access the errors property.

Note : the errors property can be evaluated at any stage when execution is paused even outside the __init__ method. This will probably not affect the result if all the fields are already fully initialized (do not forget to do this only in the __init__ method). However, you may find that you still cannot debug the form validation process because the form will be validated before you reach the is_valid method is_valid . In this case, set a breakpoint inside the full_clean method and do not stop between the form creation point and this breakpoint.

+4
source

Source: https://habr.com/ru/post/1209665/


All Articles