0

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

我能够获取组和用户,但不确定如何在我的 UserResource 中显示用户分配到的组。这是我拥有的模型资源:

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()

谢谢你的帮助。

4

1 回答 1

1

正如美味的文档所述:

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

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

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

  class Meta:
     [...]
于 2013-03-21T09:05:03.837 回答