django - How to represent unique together in Tastypie -
i have model construction keeps track of next , followers of user.
class connections(models.model): next = models.foreignkey( user, related_name='following' ) followers = models.foreignkey( user, related_name='followers' ) class meta: unique_together = (('following', 'followers'), )
post models class post(models.model): body = models.textfield() user = models.foreignkey(user) #media = models.foreignkey(postmedia) post_image = models.imagefield(upload_to=get_postimage_path) type_of_post = models.charfield(max_length=20) def __unicode__(self): homecoming u'%s, %s' % (self.user.username, self.body) class sharedpost(models.model): post = models.foreignkey(post, unique=true) date = models.datetimefield(auto_now_add=true) favors = models.integerfield(default=1) users_favored = models.manytomanyfield(user, related_name='users_favored') notify_users = models.manytomanyfield(user, related_name='notify_users') def __unicode__(self): homecoming u'%s, %s, %s' % (self.post.user.username, self.post.body, self.pk)
now if have fetch next user in views
following = [connections.followers connections in user.following.all()]
now trying design api web application using tasypie, not sure how represent relationship in modelresources.
i want generate list of followers , next in it, , utilize filter extract posts made people user following.
curl http://localhost:8000/api/v1/sharedpost/?post__user__username=abc
this using filter out posts made specific user, want filter next users.
i can use
def get_object_list(self, request): homecoming super(sharedpostresource, self).get_object_list(request).filter(post__user=request.user)
and then
def get_object_list(self, request): user=request.user next = [connections.followers connections in user.following.all()] homecoming super(sharedpostresource,self).get_object_list(request).filter(post__user__in=following).order_by('-date')
but wont have create different resources of then? there improve way that? in maybe can create 1 resource more versatile?
django tastypie
No comments:
Post a Comment