无法在Tastypie UserResource中公开“auth_user_groups”表

时间:2013-03-21 07:01:06

标签: django tastypie django-authentication

我一直在努力寻找是否有办法在我的UserResource Tastypie modelResource中公开'auth_user_groups'表。

我可以获取群组和用户,但不知道如何在我的UserResource中显示用户分配到的群组。以下是我的ModelResources:

class GroupResource(ModelResource):
class Meta:
    queryset = Group.objects.all()
    always_return_data = True
    resource_name = 'groups'
    detail_allowed_methods = ['get']
    list_allowed_methods = ['get']
    filtering = {
        'username': ALL,
    }
    authentication = ApiKeyAuthentication()
class UserResource(ModelResource):
class Meta:
    queryset = User.objects.all()
    always_return_data = True
    resource_name = 'user'
    excludes = ['is_active', 'is_staff', 'is_superuser']
    authorization = UserAuthorization() 
    detail_allowed_methods = ['get', 'post', 'put', 'delete', 'patch']
    list_allowed_methods = ['get', 'post', 'put', 'delete', 'patch']
    filtering = {
        'username': ALL,
    }
    authentication = ApiKeyAuthentication()

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

正如tastypie docs所说:

  

ModelResource子类将内省所有非关系字段

因此,您必须将群组字段添加到 UserResource

class UserResource(ModelResource):
  groups = fields.ManyToManyField(GroupResource, 'groups', null=True, full=True)

  class Meta:
     [...]