Django TastyPie 正确放置嵌套用户模型

标签 django django-models tastypie

我正在尝试使用 Django TastyPie 来更新我的模型。我有一个身份模型,充当默认 Django 用户模型的包装器:

class Identity(ProfileBase):
    user = models.OneToOneField(User, related_name='identity')
    avatar = models.ImageField(upload_to=avatar_upload_path, blank=True,
        null=True)

我有我的UserResource:

class UserResource(ModelResource):

    class Meta:
        resource_name = 'user'
        queryset = User.objects.all()
        fields = ['email', 'first_name', 'last_name']
        include_resource_uri = False

我有我的IdentityResource:

class IdentityResource(ModelResource):
    user = fields.ToOneField(UserResource, 'user', full=True)

    class Meta:
        resource_name = 'identity'
        queryset = Identity.objects.select_related()
        fields = ['user', 'avatar']
        always_return_data = True
        include_resource_uri = False

        authentication = OAuthTokenAuthentication()
        authorization = Authorization()

我目前正在使用 IdentityResource 中的 ModelResource obj_update 方法成功更新first_name、last_name:

def obj_update(self, bundle, request, **kwargs):
    print 'updating object'
    bundle = self.full_hydrate(bundle)
    bundle.obj.user = request.user
    user = bundle.data['user']
    bundle.obj.user.first_name = user['first_name']
    bundle.obj.user.last_name = user['last_name']
    return super(IdentityResource, self).obj_update(bundle, request, user=request.user)

我想要发出 PUT 请求,并可选择更新用户或身份模型上的任何字段(用户的名字、姓氏或身份上的头像字段)。我宁愿不必手动访问捆绑数据中的每个字段并手动将它们设置在模型上,就像我上面所做的那样。

我怎样才能在 TastyPie 中自然地做到这一点?有人可以解释解决这个问题的更好方法吗?非常感谢任何方向。 :)

最佳答案

以下是我提供的答案,尝试尽可能多地利用 Tastypie。

它比 OP 的请求更通用一些(它将更新任何用户,而不仅仅是登录的用户)。在现实世界中,您可能希望添加某种身份验证/授权。

from tastypie.resources import ModelResource
from tastypie.authorization import Authorization
from django.contrib.auth.models import User
from myapp.account.models import Identity

class IdentityResource(ModelResource):
    class Meta:
        queryset = Identity.objects.all()


class UserResource(ModelResource):
    class Meta:
        queryset = User.objects.all()
        allowed_list_methods = ['get']
        allowed_detail_methods = ['get','put']
        authorization = Authorization()

    def dehydrate(self, bundle):
        identity_bundle = self.build_identity_bundle(bundle)
        identity_bundle = IdentityResource().full_dehydrate(identity_bundle)
        return identity_bundle

    def obj_update(self, bundle, request, **kwargs):
        user_bundle = super(UserResource, self).obj_update(bundle, request, **kwargs)
        identity_bundle = self.build_identity_bundle(user_bundle)
        IdentityResource().obj_update(identity_bundle, request)
        return user_bundle

    def build_identity_bundle(self, user_bundle):
        identity_bundle = IdentityResource().build_bundle(
                obj=user_bundle.obj.get_profile(),
                data=user_bundle.data
            )
        return identity_bundle

示例支持的是:

  • 获取扁平化的用户+身份资源
  • PUT 扁平化的用户+身份资源,更新两个模型

您可能希望在 API 中注册 UserResource,而不是 IdentityResource。

关于Django TastyPie 正确放置嵌套用户模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11942245/

相关文章:

python - 无法在 Django 中分配用户

python - 消息系统的 Django ORM 查询

python - 在 dehydrate 中序列化查询集

Django继承基类super

python - django 模板中的表单字段值

python - Django : is it better to import variables from a settings. py文件,还是基本配置文件?

django - 如何在django中使用多表继承复制对象

Django 1.7 迁移

django - Backbone.js 发布 500 错误

python - Django 错误 : cannot open shared file