(I'm porting an app and originally posted this in its mailing list but it's
been a day and I haven't gotten a response. If that's not a good enough
reason for cross-posting, sorry and feel free to ignore me.)
I'm using python 3.3 and django 1.6 .
I have this code:
with self.assertNumQueries(1) as q:
all_entries = list(Entry.objects.all())
print("All entries: ", all_entries)
for entry in all_entries:
print("Entry: ", entry, " , Tags list: ", entry.tags_list)
for query in q.captured_queries:
print(query, "\n")
which produces this output (slightly edited for prettiness):
All entries: [<Entry: My entry: published>]
Entry: My entry: published , Tags list: []
{'time': '0.000', 'sql': 'QUERY = \'SELECT "zinnia_entry"."id",
"zinnia_entry"."title", "zinnia_entry"."slug", "zinnia_entry"."status",
"zinnia_entry"."start_publication", "zinnia_entry"."end_publication",
"zinnia_entry"."creation_date", "zinnia_entry"."last_update",
"zinnia_entry"."content", "zinnia_entry"."comment_enabled",
"zinnia_entry"."pingback_enabled", "zinnia_entry"."trackback_enabled",
"zinnia_entry"."comment_count", "zinnia_entry"."pingback_count",
"zinnia_entry"."trackback_count", "zinnia_entry"."excerpt",
"zinnia_entry"."image", "zinnia_entry"."featured", "zinnia_entry"."tags",
"zinnia_entry"."login_required", "zinnia_entry"."password",
"zinnia_entry"."content_template", "zinnia_entry"."detail_template" FROM
"zinnia_entry" ORDER BY "zinnia_entry"."creation_date" DESC\' - PARAMS =
()'}
{'time': '0.000', 'sql': 'QUERY = \'SELECT "tagging_tag"."id",
"tagging_tag"."name" FROM "tagging_tag" INNER JOIN "tagging_taggeditem" ON
( "tagging_tag"."id" = "tagging_taggeditem"."tag_id" ) WHERE
("tagging_taggeditem"."content_type_id" = %s AND
"tagging_taggeditem"."object_id" = %s ) ORDER BY "tagging_tag"."name" ASC\'
- PARAMS = (12, 1)'}
(I get the same queries if I don't print out the entry or its tag list. I
just retained the print loop to show that it doesn't have any tags)
Entry.objects is the standard db.models.Manager type and as far as I can
tell, it has not been monkeypatched anywhere.
Obviously, I got two queries instead of just the one that I was expecting.
I can't find any reason for the second query. I'm especially confused
because the entry clearly doesn't even have a tag (as demonstrated by the
empty tags_list). Would anyone happen to have any idea how querying for one
model can lead to a query for another model type? If not, do you have any
suggestions for me in debugging the issue?
I've already checked if the __str__ method of the Model somehow references
the object mentioned in the second query and it doesn't. (I can remove the
print calls and it'll still be triggered anyway.)
Thanks
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.