Skip to content

Instantly share code, notes, and snippets.

@soukiassianb
Last active August 29, 2015 14:13
Show Gist options
  • Save soukiassianb/b8fb172a7ddcad12906c to your computer and use it in GitHub Desktop.
Save soukiassianb/b8fb172a7ddcad12906c to your computer and use it in GitHub Desktop.
Filter product by tag [TaggedProduct classmethod]
class TaggedProducts(GenericTaggedItemBase):
[...]
# filter products by tags
@classmethod
def filter(cls, *tags):
products = Products.objects.all()
for tag in tags:
if tag not in (t.slug for t in ProductTags.objects.all()): # not good for speed
return "%s tag does not exists, please try with valid tags" % (tag)
products = products.filter(tags__slug__in=[tag])
return products if len(products) > 0 else "No products tagged with %s" % (", ".join(tags))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment