0

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

ご協力いただきありがとうございます。

4

1 に答える 1

1

おいしいパイのドキュメントで述べられているように:

ModelResource サブクラスはすべての非リレーショナル フィールドをイントロスペクトします

したがって、 groupsフィールドをUserResourceに追加する必要があります。

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

  class Meta:
     [...]
于 2013-03-21T09:05:03.837 に答える