Question

I am relatively new to Django and have really been struggling with an implementation of a custom django-taggit app through the tastypie REST API. I have researched this and keep running into the same issues. I appreciate any help and guidance you can provide.

I have a Model that I am trying to add Tags to with django-taggit. I needed to add a user_id to each tag so that each user owned their own list of tags. For this reason I setup a Through Model as shown here. Here is how my models are setup:

class Tags(TagBase):
    user = models.ForeignKey('UserProfile')

class TaggedMedia(GenericTaggedItemBase):
    tag = models.ForeignKey(Tags, related_name="tagged_items")

class Media(models.Model):
    user = models.ForeignKey('UserProfile')
    # All Other Media Fields
    tags = TaggableManager(through=TaggedMedia)

This sets up the Database tables as I thought it would, so I feel like I am on the right path.

Now, when I try to access these models via TastyPie, This is where I am running into issues. My Model Resource is setup as follows:

class TaggedResource(ModelResource):
    tags = ListField()
    user = fields.ForeignKey(UserProfileResource, 'user')

    class Meta:
        queryset = Media.objects.all().order_by('-timestamp').distinct()
        authorization = MediaAuthorization()
        detail_allowed_methods = ['get', 'post', 'put', 'delete','patch']

    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)

Now. This works great for 2 scenarios:

  1. Running a GET query for a query with Tag filters.
  2. Running a PUT query to add EXISTING tags to the TaggedMedia table

However, if I run a PUT query with a Tag that is not in the Tags table already, it will hang and spin forever with no response.

Sorry, longest question ever, but hopefully the details will help you help me. :) Thanks again!

Was it helpful?

Solution

I am sure this is not optimal, but this seems to work:

def save_m2m(self, bundle):
    usrObj = User.objects.get(username=bundle.request.GET['username'])
    tags = bundle.data.get('tags', [])
    print tags

    for tag in tags:
        #check if the tag already exists for that user, if not save it in the DB before we try to reference it with m2m
        try:
            tagCheck = Tags.objects.get(user_id=usrObj.id,name=tag)
        except Tags.DoesNotExist:
            tagCheck = None
        if tagCheck is None:
            t1 = Tags(name=tag, user_id=usrObj.id)
            t1.save()
    #create relationships between tags and media        
    bundle.obj.tags.set(*tags)
    return super(MediaResource, self).save_m2m(bundle)

I quickly check to see if the tag exists, if not, I create it quickly. This works and doesn't crash it. Do you see any problems with doing this?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top