Skip to content

Instantly share code, notes, and snippets.

@jaredlewis
Last active December 14, 2015 00:29
Show Gist options
  • Save jaredlewis/4999393 to your computer and use it in GitHub Desktop.
Save jaredlewis/4999393 to your computer and use it in GitHub Desktop.
# Get a list of the entities
entities = Entity.objects.filter(id__in=topic_ids)
# Build the query to get the entity instances
entity_story_query = EntityInstance.objects.filter(
entity_id__in=topic_ids,
story__content_type__app_label__iexact='sources',
story__content_type__model__iexact='xmlfeedsource',
).distinct(
'story__update_time',
'story__id'
).select_related(
'story',
'story__topics__entity'
'entity',
).prefetch_related(
'story__source',
'story__topics',
'story__topics__entity__groups',
'story__media_assets'
).order_by(
*order
)
# Get the result set
entity_stories_result = entity_story_query[offset:(offset + num_stories)]
# Get the entity stories
entity_stories = [entity_instance.story for entity_instance in entity_stories_result]
# Get the sources
source_ids = [story.source.pk for story in entity_stories]
sources = XmlFeedSource.objects.filter(
pk__in=source_ids
).prefetch_related(
'uris',
'categories',
'tags'
)
# Replace the sources of the stories
for story in entity_stories:
story.source = [source for source in sources if source.pk == story.source.pk][0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment