Skip to content

Instantly share code, notes, and snippets.

@issackelly
Created July 12, 2012 05:38
Show Gist options
  • Save issackelly/3096057 to your computer and use it in GitHub Desktop.
Save issackelly/3096057 to your computer and use it in GitHub Desktop.
>>> from taggit.models import Tag
>>> Tag.objects.all()
[<Tag: Administrative>, <Tag: Philantrophy>, <Tag: Fundraising>, <Tag: Management>, <Tag: Food Preparation/Cooking>, <Tag: Labor>, <Tag: Food Service>, <Tag: Musician>, <Tag: Emergency Services>, <Tag: Medical Services>, <Tag: Leadership>, <Tag: Teaching>, <Tag: Manual Labor>, <Tag: Cleaning>, <Tag: Clerical>, <Tag: Math>, <Tag: Science>, <Tag: Law>, <Tag: Spanish>, <Tag: Social Justice>, '...(remaining elements truncated)...']
>>> OpportunityNeed.objects.filter(skills__name='Leadership').query
<django.db.models.sql.query.Query object at 0x2b2ad10>
>>> print OpportunityNeed.objects.filter(skills__name='Leadership').query
SELECT "opportunities_opportunityneed"."id", "opportunities_opportunityneed"."created", "opportunities_opportunityneed"."modified", "opportunities_opportunityneed"."kind", "opportunities_opportunityneed"."number", "opportunities_opportunityneed"."location_id", "opportunities_opportunityneed"."event_id" FROM "opportunities_opportunityneed" INNER JOIN "opportunities_opportunityneed_skills" ON ("opportunities_opportunityneed"."id" = "opportunities_opportunityneed_skills"."opportunityneed_id") INNER JOIN "taggit_tag" ON ("opportunities_opportunityneed_skills"."tag_id" = "taggit_tag"."id") WHERE "taggit_tag"."name" = Leadership
>>> print OpportunityNeed.objects.filter(skills__name='Leadership', skills__name='Teaching').query
File "<console>", line 1
SyntaxError: keyword argument repeated (<console>, line 1)
>>> print OpportunityNeed.objects.filter(skills__name='Leadership').filter(skills__name='Teaching').query
SELECT "opportunities_opportunityneed"."id", "opportunities_opportunityneed"."created", "opportunities_opportunityneed"."modified", "opportunities_opportunityneed"."kind", "opportunities_opportunityneed"."number", "opportunities_opportunityneed"."location_id", "opportunities_opportunityneed"."event_id" FROM "opportunities_opportunityneed" INNER JOIN "opportunities_opportunityneed_skills" ON ("opportunities_opportunityneed"."id" = "opportunities_opportunityneed_skills"."opportunityneed_id") INNER JOIN "taggit_tag" ON ("opportunities_opportunityneed_skills"."tag_id" = "taggit_tag"."id") INNER JOIN "opportunities_opportunityneed_skills" T4 ON ("opportunities_opportunityneed"."id" = T4."opportunityneed_id") INNER JOIN "taggit_tag" T5 ON (T4."tag_id" = T5."id") WHERE ("taggit_tag"."name" = Leadership AND T5."name" = Teaching )
>>> from django.models import Q
Traceback (most recent call last):
File "<console>", line 1, in <module>
ImportError: No module named models
>>> from django.db.models import Q
>>> print OpportunityNeed.objects.filter(Q(skills__name='Leadership') & Q(skills__name='Teaching')).query
SELECT "opportunities_opportunityneed"."id", "opportunities_opportunityneed"."created", "opportunities_opportunityneed"."modified", "opportunities_opportunityneed"."kind", "opportunities_opportunityneed"."number", "opportunities_opportunityneed"."location_id", "opportunities_opportunityneed"."event_id" FROM "opportunities_opportunityneed" INNER JOIN "opportunities_opportunityneed_skills" ON ("opportunities_opportunityneed"."id" = "opportunities_opportunityneed_skills"."opportunityneed_id") INNER JOIN "taggit_tag" ON ("opportunities_opportunityneed_skills"."tag_id" = "taggit_tag"."id") WHERE ("taggit_tag"."name" = Leadership AND "taggit_tag"."name" = Teaching )
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment