Hi,
On Jun/19/2020, Carlton Gibson wrote:
> Bon dia Carles.
I'm very impressed! :-)
> I'm inclined to say that calling self.errors in __init__ is at least *holding
> it wrong*.
:-) it almost worked! (the only known issue was for the BaseForm not
deleting the model of my ModelForm... for what I can see though, it
might have other problems!).
> I'm not sure what if anything we might do about that. DRF's serializers
> have all kinds of
> "you have to call is_valid() before accessing .data" type assertions, but
I'm not familiar with the internals of DRF (Django REST Framework I
understand) :-( yet :-)
In Django I see that is_valid() is only "return is_bound and not self.errors:"
https://github.com/django/django/blob/master/django/forms/forms.py#L177
So I didn't think that to access .data I should call is_valid(). Anyway,
what I want to do is to avoid using .data at all.
Yes, I did this (if there is a BooleanField enabled I ignore some checks
in the clean)
My main problem was: ModelSet has a form that is not valid. When the
BaseForm (ModelForm in my case) is constructed again (because is_valid() is
False so the view reloads the form again showing errors to the user): if
it's not valid for a specific reason I need to add a BooleanField (to do
what you said in the clean(): ignore one of the validations).
Yesterday I implemented: in the __init__() get the data using .data (I
don't like it though) and execute my logic again to determine the cause
of the error. My first idea was to access .errors and look for a
specific error (then the problem is that the FormSet was not deleting
the Model if the user clicked on deleting a form from the formset)
For what I can see in Django code: If BaseFormSet.add_fields() called
form.full_clean() (only needed if add_fields had added a new field): I
think that if the user deletes a form (so f{prefix}-DELETE='on' is
received) the Model that the Form holds (in my case it is a ModelForm)
would have been deleted as expected. I don't know if this is going to
backfire in some places or some use cases.
(BaseFormSet.add_fields for reference:
https://github.com/django/django/blob/master/django/forms/formsets.py#L373 )
Sorry if this sounds like a brain twist!
If using .errors in the form __init__ is unsupported: no problem!
Thanks to this email I have an idea on how to avoid it!
I'll check if the documentation says that and I've missed it.
If using .errors in the form __init__ is supported I might look a bit
more into this.
Cheers,
> --
> You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
django-develop...@googlegroups.com.
> To view this discussion on the web visit
https://groups.google.com/d/msgid/django-developers/3ed8045c-52b8-4647-9fe6-c0326aaf923do%40googlegroups.com.