1

I wish to return MultipleObjects but I'm not sure how as I keep getting this error:

MultipleObjectsReturned at /clubs/
get() returned more than one Player -- it returned 2!

I want it to return a list of all the players that belong to the club but it only returns one (if I remove all the other players from my database but when I add the rest of the team it throws this error) I'm not sure how to make get return multiple objects.

models.py

class Club(models.Model):
    name = models.CharField(max_length=4096)
    abbreviation = models.CharField(max_length=4096)
    def __str__(self):
        if self.player:
            return self.player.name
        return self.name

class Player(models.Model):
    name = models.CharField(max_length=4096)
    age = models.IntegerField()
    goals = models.IntegerField()
    club = models.OneToOneField(Club, null=True, related_name='player')

    def __str__(self):
        return self.name

views.py

class ClubViewSet(viewsets.ModelViewSet):
    queryset = Club.objects.order_by('name')
    serializer_class = ClubSerializer

class PlayerViewSet(viewsets.ModelViewSet):
    queryset = Player.objects.order_by('name')
    serializer_class = PlayerSerializer

serializer.py

class ClubSerializer(serializers.HyperlinkedModelSerializer):
   player = serializers.HyperlinkedRelatedField(
      many=False,
      read_only=True,
      view_name='player-detail'
   )
   class Meta:
      model = Club
      fields = ('url','name', 'abbreviation','player')

class PlayerSerializer(serializers.HyperlinkedModelSerializer):
   class Meta:
      model = Player
      fields = ('url', 'name', 'age', 'goals')
2
  • 2
    Use filter instead of get. Commented Apr 1, 2016 at 10:21
  • @SteinarLima that's not very helpful in this case -- there is no get() in the OPs code. Commented Apr 1, 2016 at 10:33

1 Answer 1

3

You have a one to one field from player to club.

class Player(models.Model):
    club = models.OneToOneField(Club, null=True, related_name='player')

You'll also need to change your club's __str__ method, because it currently uses self.player. I suggest you use the club's name rather than including all the players' names.

class Club(models.Model):
    ...
    def __str__(self):
        returns self.name

This means that each player belongs to one club, and each club belongs to one player.

It would make more sense to use a foreign key instead. That means each player belongs to one club, but many players can belong to the same club.

class Player(models.Model):
    club = models.ForeignKey(Club, null=True, related_name='players')

You'll have to create a migration and run it after making this change.

Then update your serializers to support multiple players.

class ClubSerializer(serializers.HyperlinkedModelSerializer):
    player = serializers.HyperlinkedRelatedField(
        many=True,
        read_only=True,
        view_name='player-detail'
    )
    ...
Sign up to request clarification or add additional context in comments.

3 Comments

coercing to Unicode: need string or buffer, NoneType found I get this error now
Sorry, I forgot that now I had to change str to get_queryset.
I wouldn't recommend including the players in the __str__ method. Using return self.name might be a better idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.