0
class UserProfile(models.Model):
"""
Extending the Django User.
"""
user = models.OneToOneField(User,unique=True)
image_id = models.CharField(max_length=50,blank=True)
is_email_validated = models.BolleanField(default = False)
followers_count = models.IntegerField(max_length=5,default=0,blank=True,null=True)
following_users = ModelListField()
follower_users = ModelListField()
def __unicode__(self):
return u"%s" % self.user.first_name
def full_name(self):
return u"%s" % self.user.first_name + '' + self.user.last_name
def email(self):
return self.user.email
def username(self):
return self.user.username
def books_count(self):
return len(self.user.get_profile().books)
def create_user_profile(sender,instance,created,**kwargs):
if created:
UserProfile.objects.create(user=instance)
action.send(type="create_user",actor=instance,target=instance)
post_save.connect(create_user_profile,sender=User)
因此,我該如何製作api? (我非常困惑Django tastypie如何在這種情況下工作)如何使用django tastypie的處理程序爲UserProfile製作apis?
我可以編寫處理程序來返回api調用的數據,如GET/users // following。但是,這是如何在tastypie中完成的?如果是的話,我該如何連接處理程序和API?
編輯: 像這樣的東西可以在Django活塞就像這樣:
from piston.handler import BaseHandler
from piston.utils import rc
from app.apps.user.models import User, UserProfile
class UserHandler(BaseHandler):
model = User
def read(self, request, user_id=None, action=None):
"""
GET /users/
following - GET /users/<user_id>/following
followers - GET /users/<user_id>/followers
user profile - GET /users/<user_id>/
"""
if action =="following":
return self.following(request, user_id)
elif action == "followers":
return self.followers(request, user_id)
else:
return self.get_user_profile(request, user_id)
def get_user_profile(self, request, user_id):
"""
User Profile
GET /users/<user_id>/
"""
user = User.objects.get(pk=user_id)
user_session = request.user
user_session_following = user_session.get_profile().following_users
if user.id in user_session_following:
follow_status=True
else:
follow_status=False
return user_actions.get_user_profile(user, follow_status)
混亂不是這樣。這個我理解得很好,並且已經實現了。我面臨的問題是處理諸如get/api/user_profile/users之類的請求以獲取所有用戶。這是否意味着我必須爲每個這樣的API端點編寫資源? – Hick
我編輯了我的帖子,使其更加精確。 – jazz