I am having a strange behaviour that I cannot find out why it's happening.
I have a simple queryset with a deferred field, for example Person.objects.filter(id=4).defer('phone') and then I have a test that asserts this:
with self.assertNumQueries(2):
p = Person.objects.filter(id=4).defer('phone').first() # 1 query
p.phone # 1 query
It fails, because it seems to run three queries on that block: the first one when filtering, and two more duplicate queries that come from the p.phone statement (SELECT phone FROM ...).
Does anyone have any idea why this is happening?
- Note: i'm using Django 2.0. And it also happens using
only(), the counterpart ofdefer().