How to filter a field in a related object?

If I try to filter a field in a related object, then Tastypie returns an error. For example, launch

curl -H "Accept: application/json" \
     "http://localhost:8080/wordgame/api/v1/rounds/?format=json&players__username=moe"

returns “Search is not allowed more than one level in the players field. Essentially, I'm trying to do what I can do now in the Django shell:

Round.objects.all().filter(players__username=moe.username)

I use the following code, which I simplified for brevity:

# wordgame/api.py which has tastypie resources
class RoundResource(ModelResource):
    players = fields.ManyToManyField(UserResource, 'players',full=True)
    . . .

    class Meta:
        queryset = Round.objects.all()
        resource_name = 'rounds'
        filtering = {
            'players': ALL,
        }

class UserResource(ModelResource):
    class Meta:
        queryset = User.objects.all()
        resource_name = 'players'
        filtering = {
            'username': ALL,
        }

# wordgame/models.py which has Django models
class Round(models.Model):
    players = models.ManyToManyField(User)
    word = models.CharField(max_length=75)
    . . . 

I assume that since UserResource defines a filter in the "username" field, this should work, but it is not. I even tried adding "players__username" to the filter in RoundResource, however that didn't work either.

GitHub, . , , . Tastypie GitHub, , , 1) , 2) , .

+5
1

-, , , filtering, :

class UserResource(ModelResource):
    class Meta:
        queryset = User.objects.all()
        resource_name = 'players'
        filtering = {
            'username': ALL_WITH_RELATIONS,
        }

, , . . Tastypie, , , .

+11

All Articles