Skip to content

Instantly share code, notes, and snippets.

@bohde
Created January 30, 2012 03:25
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save bohde/1702293 to your computer and use it in GitHub Desktop.
Save bohde/1702293 to your computer and use it in GitHub Desktop.
Using django-taggit with django-tastypie
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 = bundle.data.get('tags', [])
bundle.obj.tags.set(*tags)
return super(TaggedResource, self).save_m2m(bundle)
@jonykalavera
Copy link

thank you so much man!

@zxl777
Copy link

zxl777 commented Nov 6, 2013

thanks ,but how to use it?

How to add a tag into a obj's tags?

And How to post a new obj with tags?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment