def attach_epic_order(queryset, epic_id, as_field='epic_order'):
 model = queryset.model
 sql = 'SELECT "epics_relateduserstory"."order" AS "epic_order"\n               FROM "epics_relateduserstory"\n              WHERE "epics_relateduserstory"."user_story_id" = {tbl}.id and\n                    "epics_relateduserstory"."epic_id" = {epic_id}'
 sql = sql.format(tbl=model._meta.db_table, epic_id=epic_id)
 queryset = queryset.extra(select={as_field: sql})
 return queryset

def attach_epic_order ( queryset , epic_id , as_field = 'epic_order' ) : model = queryset . model sql = 'SELECT "epics_relateduserstory"."order" AS "epic_order"\n               FROM "epics_relateduserstory"\n              WHERE "epics_relateduserstory"."user_story_id" = {tbl}.id and\n                    "epics_relateduserstory"."epic_id" = {epic_id}' sql = sql . format ( tbl = model . _meta . db_table , epic_id = epic_id ) queryset = queryset . extra ( select = { as_field : sql } ) return queryset
attach epic_order column to each object of the queryset .