从tastypie获取用户特定数据

时间:2014-04-10 13:36:32

标签: django tastypie

我想在Tastypie中仅获取经过身份验证的用户特定数据,但我不知道是否可以使用会话或其他内容来存储用户详细信息。以下是我的代码:

class PhonebookValidation(Validation):
    """Phonebook Validation Class"""
    def is_valid(self, bundle, request=None):
        if not bundle.data:
            return {'__all__': 'Please enter data'}
        errors = {}
        if not 'name' in bundle.data or bundle.data.get('name') == '':
            errors['name'] = ['Please enter phonebook name.']
        return errors

class PhonebookResource(ModelResource):
    user = fields.ForeignKey(UserResource, 'user')
    class Meta:
        queryset = Phonebook.objects.all()
        resource_name = 'phonebook'
        authorization = Authorization()
        authentication = BasicAuthentication()
        validation = PhonebookValidation()
        throttle = BaseThrottle(throttle_at=1000, timeframe=3600)

    def hydrate(self, bundle, request=None):
        bundle.obj.user = User.objects.get(pk=bundle.request.user.id)
        return bundle

    def obj_create(self, bundle, **kwargs):
        return super(PhonebookResource, self).obj_create(bundle, user=bundle.request.user)

    def apply_authorization_limits(self, request, object_list):
        return object_list.filter(user=request.user)

但它仍然会返回所有用户的数据。如果我做错了或者建议我使用一些代码/链接来理解它,请指导我。

0 个答案:

没有答案