0

I have been struggling to find out if there is way to expose the 'auth_user_groups' table in my UserResource Tastypie modelResource.

I am able to get the Groups and Users, but not sure how to show the Groups that a User is assigned to within my UserResource. Here are the ModelResources I have:

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

Thanks for your help.

bevinlorenzo
  • 446
  • 6
  • 18

1 Answers1

1

As stated by the tastypie docs :

ModelResource subclass will introspect all non-relational fields

So you must add the groups field to your UserResource :

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

  class Meta:
     [...]
Ponytech
  • 1,567
  • 11
  • 19