2012-10-14 34 views
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) 

回答

1

把你的應用程序/應用/用戶/ api.py

from tastypie.resources import ModelResource 
from django.contrib.auth.models import User 

class UserResource(ModelResource): 
    class Meta: 
     queryset = User.objects.all() 
     resource_name = 'user' 

而在你的網址.py

from django.conf.urls.defaults import * 
from app.apps.user.api import UserResource 

v1_api = Api(api_name='v1') #api_name will be 'v1' in http://localhost:8000/v1/?format=json 
v1_api.register(UserProfileResource())  

urlpatterns = patterns('', 
    (r'^api/', include(v1_api.urls)), 
) 

我不是故意在這裏粗俗,而是閱讀文檔,特別是quickstart,會有所幫助。

你的API現在(在你的devserver)暴露

http://localhost:8000/api/v1/?format=json 

這就是所有。你不必寫任何代碼,例如

- retrieve all users in json: 
    http://localhost:8000/api/v1/user/?format=json 

- retrieve the entry in the user table with the key 1 
    http://localhost:8000/api/v1/user/1/?format=json 

- retrieve the meta information about your UserResource 
    http://localhost:8000/api/v1/user/schema/?format=json 

希望這可以幫助你!

+0

混亂不是這樣。這個我理解得很好,並且已經實現了。我面臨的問題是處理諸如get/api/user_profile/users之類的請求以獲取所有用戶。這是否意味着我必須爲每個這樣的API端點編寫資源? – Hick

+0

我編輯了我的帖子,使其更加精確。 – jazz