Skip to content

Instantly share code, notes, and snippets.

@justquick
Created April 12, 2010 13:56
Show Gist options
  • Save justquick/363577 to your computer and use it in GitHub Desktop.
Save justquick/363577 to your computer and use it in GitHub Desktop.
def latest_objects_by_category(category, app_label, model_name, set_name, date_field='pub_date'):
from django.db.models import get_model
from categories import registry
from categories.models import Category
if isinstance(category, basestring):
category = Category.objects.get(slug=category)
m = get_model(app_label, model_name)
fk = hasattr(category, 'primary_%s_set'%set_name) and getattr(category, 'primary_%s_set'%set_name).all() or set()
m2m = hasattr(category, '%s_set'%set_name) and getattr(category, '%s_set'%set_name).all() or set()
results = fk | m2m
return results.order_by('-%s' % date_field)
NATIVE_LIBRARY = {
'function':{
'get_latest_objects_by_category': latest_objects_by_category
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment