pytest-django icon indicating copy to clipboard operation
pytest-django copied to clipboard

fail-on-template-vars: improve compatibility with Django behavior

Open xavfernandez opened this issue 7 months ago • 3 comments

With OneToOneField, Django raises Model.DoesNotExist which is converted by its template engine to string_if_invalid: https://github.com/django/django/blob/5.0.7/django/template/base.py#L932-L933

It is usually falsy, hence the need for InvalidVarException.__bool__ to return bool(self.origin_value) to be consistent with Django's default behavior.

However to trigger InvalidVarException behavior and its dreaded InvalidVarException.__mod__, it needs to go through this check: https://github.com/django/django/blob/5.0.7/django/template/base.py#L716 and thus also needs to be truthy hence the stack inspecting __bool__ method to know what to return.

xavfernandez avatar Jul 10 '24 09:07 xavfernandez