Skip to content

Instantly share code, notes, and snippets.

@prokaktus
Created December 3, 2017 23:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prokaktus/bd805e3f9a3eabb64be22523ae5fdf66 to your computer and use it in GitHub Desktop.
Save prokaktus/bd805e3f9a3eabb64be22523ae5fdf66 to your computer and use it in GitHub Desktop.
Workaround for Django-Taggit `KeyError` while calling `similar_objects`
# Just example. Feel free to use it and modify to your needs.
# ArticlePage - my model
# ArticleTag - m2m model for my tags (TaggedItemBase)
# taggit_tag - Taggit tag model
# (1, 2, 3) - just example tag ids
ArticlePage.objects.raw('select a.page_ptr_id, p.title, count(at.tag_id) as tag_count from article_articlepage a join wagtailcore_page p on a.page_ptr_id = p.id join article_articletag at on at.content_object_id = a.page_ptr_id join taggit_tag t on t.id = at.tag_id where tag_id in (1, 2, 3) group by (page_ptr_id, p.id) order by tag_count desc');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment