3

I modeled like as a DB table

class UserLikeAlbumImage(models.Model):
    user = models.ForeignKey(User)
    album_image = models.ForeignKey(AlbumImage)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        unique_together = ('user', 'album_image')

currently I have

api/v1/userlikealbumimage/
api/v1/userlikealbumimage/3 # 3 is UserLikeAlbumImage's id

I want to have

/like/user/{user-id}/image/{image-id} from The REST-way to check/uncheck like/unlike favorite/unfavorite a resource

Especially since, it's natural to think like as a composition of (user, liked_object).

/like/user/{user-id}/image/{image-id} 
GET: find out if user likes image
POST: `create` user likes image
DELETE: `delete` user likes image

/like/user/{user-id}/  
GET: list all images a user likes

/like/image/{image-id}/
GET: list all users who like this image

So I assume this addressing scheme could be expressible with django-tastypie, how do I get started? (what should I search for?)

--- EDIT ---

ok I made it work by

  def override_urls(self):
        return [
            url(r"^(?P<resource_name>%s)/user/(?P<user__id>\d+)/image/(?P<album_image__id>\d+)/$" % self._meta.resource_name,
                self.wrap_view('dispatch_detail'), name="api_dispatch_detail"),
            ]

The question now is, is this restful?

Community
  • 1
  • 1
eugene
  • 39,839
  • 68
  • 255
  • 489

0 Answers0