Skip to content

Instantly share code, notes, and snippets.

@jonykalavera
Forked from bohde/tags.py
Last active December 18, 2015 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonykalavera/5864537 to your computer and use it in GitHub Desktop.
Save jonykalavera/5864537 to your computer and use it in GitHub Desktop.
from taggit.utils import parse_tags # with this you can accept tags as a string to be parsed :)
from tastypie.fields import ListField
class TaggedResource(ModelResource):
tags = ListField()
class Meta:
queryset = Model.objects.all()
def build_filters(self, filters=None):
if filters is None:
filters = {}
orm_filters = super(TaggedResource, self).build_filters(filters)
if 'tag' in filters:
orm_filters['tags__name__in'] = filters['tag'].split(',')
return orm_filters
def dehydrate_tags(self, bundle):
return map(str, bundle.obj.tags.all())
def save_m2m(self, bundle):
tags = parse_tags(bundle.data.get('tags', ''))
bundle.obj.tags.set(*tags)
return super(TaggedResource, self).save_m2m(bundle)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment