Skip to content

Instantly share code, notes, and snippets.

@jaredlewis
Created June 17, 2013 19:51
Show Gist options
  • Save jaredlewis/5799744 to your computer and use it in GitHub Desktop.
Save jaredlewis/5799744 to your computer and use it in GitHub Desktop.
class SubjectStoryQuerySet(QueryBuilderQuerySet):
class Meta:
model = 'stories.Story'
def __init__(self, *args, **kwargs):
super(SubjectStoryQuerySet, self).__init__(*args, **kwargs)
# Create the intermediate queries
self.subject_stories_query = Query().from_table(
table='subjects_subject_topics',
fields=None
).join(
right_table='subjects_storytopicinstance',
prefix_fields=False,
fields=[
"story_id",
MaxField(field="relevance", alias="relevance")
],
condition='subjects_subject_topics.topic_id = subjects_storytopicinstance.topic_id'
).group_by(
field='subjects_storytopicinstance.story_id'
)
self.q = Query().from_table(
table={
"subject_stories": self.subject_stories_query
},
fields=None
).join(
right_table='stories_story',
prefix_fields=False,
fields=[
'id'
],
condition='stories_story.id = subject_stories.story_id'
)
def filter__subject_id(self, filter, expression, value):
print "---filter subject id"
self.subject_stories_query.where(Q(
**filter
))
def order__(self, field, desc):
self.q.order_by(field=field, desc=desc)
def get_model_queryset(self, queryset, offset, limit):
self.q.limit(limit=limit, offset=offset)
return queryset.filter(
pk__in=[
story.get('id')
for story
in self.q.select()
]
)
def count(self):
return self.q.limit(None, None).count()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment