尝试通过Django REST Framework创建时出现ValueError

时间:2019-06-21 06:14:46

标签: python django django-rest-framework

我得到一个ValueError Cannot assign "[]": "Match.stats" must be a "Stats" instance。当我尝试通过可浏览的API创建匹配项时,但可以通过shell创建一个匹配项。

如果我从MatchSerializer中删除HyperlinkedRelatedField,它可以很好地创建。

models.py

class Player(models.Model):
    name = models.CharField(max_length=30)
    account = models.IntegerField()
    place = models.CharField(max_length=30)
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='players')

    def __str__(self):
        return self.name


class Match(models.Model):
    game = models.IntegerField()
    length = models.IntegerField()
    win = models.BooleanField()
    player = models.ForeignKey(Player, on_delete=models.CASCADE, related_name='matches')

    def __str__(self):
        return str(self.game)



class Stats(models.Model):
    goals = models.IntegerField()
    assists = models.IntegerField()
    time = models.IntegerField()
    match = models.OneToOneField(Match, on_delete=models.CASCADE, related_name='stats')

    def __str__(self):
         return '{} {}'.format(str(self.goals), str(self.match))


class Team(models.Model):
    possession = models.IntegerField()
    goals = models.IntegerField()
    assists = models.IntegerField()
    extra = models.BooleanField(default=False)
    match = models.OneToOneField(Match, on_delete=models.CASCADE, related_name='teams')

    def __str__(self):
        return '{} - {}'.format(str(self.possession), str(self.match))

serializer.py

class UserSerializer(serializers.ModelSerializer):
    players = serializers.HyperlinkedRelatedField(many=True, view_name='players-detail', queryset=Player.objects.all())
    class Meta:
        model = User
        fields = ('id', 'username', 'email', 'first_name', 'last_name', 'players')


class PlayerSerializer(serializers.ModelSerializer):
    user = serializers.ReadOnlyField(source='user.username')
    matches = serializers.HyperlinkedRelatedField(many=True, view_name='matches-detail', queryset=Match.objects.all())
    class Meta:
        model = Player
        fields = ('id', 'name', 'account', 'place', 'user', 'matches')


class MatchSerializer(serializers.ModelSerializer):
    player = serializers.ReadOnlyField(source='player.name')
    stats = serializers.HyperlinkedRelatedField(many=True, view_name='stats-detail', queryset=Stats.objects.all())
    teams = serializers.HyperlinkedRelatedField(many=True, view_name='teams-detail', queryset=Team.objects.all())
    class Meta:
        model = Match
        fields = ('id', 'game', 'length', 'win', 'player', 'stats', 'teams')

class StatsSerializer(serializers.ModelSerializer):
    match = serializers.ReadOnlyField(source='match.game')
    class Meta:
        model = Stats
        fields = ('id', 'goals', 'assists', 'time', 'match')


class TeamSerializer(serializers.ModelSerializer):
    match = serializers.ReadOnlyField(source='match.game')
    class Meta:
        model = Team
        fields = ('id', 'possession', 'goals', 'assists', 'extra', 'match')

我可以进入python manage.py shell并创建一个匹配项。

>>>m = Match(game=12345, length=5674, win=True, player=player1) # a previously queried player 
>>>m.save()  

我只是有点困惑

1 个答案:

答案 0 :(得分:1)

默认情况下,HyperlineRelatedField允许读写操作,因此我认为您需要将其设为只读true:

class MatchSerializer(serializers.ModelSerializer):
    player = serializers.ReadOnlyField(source='player.name')
    stats = serializers.HyperlinkedRelatedField(view_name='stats-detail', read_only=True)
    teams = serializers.HyperlinkedRelatedField(view_name='teams-detail', read_only=True)
    class Meta:
        model = Match
        fields = ('id', 'game', 'length', 'win', 'player', 'stats', 'teams')

此外,您无需添加many=True,因为团队和统计信息都是OneToOne关系。因此,每次匹配都会为两个表创建一个条目。