我正在尝试为Django Rest Framework的tutorial中的代码添加投票功能。在Snippet模型之上,我添加了一个投票模型:
class Vote(models.Model):
created = models.DateTimeField(auto_now_add=True)
voter = models.ForeignKey(User, on_delete=models.CASCADE)
snippet = models.ForeignKey(Snippet, related_name='votes', on_delete=models.CASCADE)
class Meta:
ordering = ('created',)
在我的序列化程序中,我试图验证用户不能多次投票而不能投票给他自己的代码段的事实:
class VoteSerializer(serializers.HyperlinkedModelSerializer):
voter = serializers.ReadOnlyField(source='voter.username',validators=[UniqueValidator(queryset=Vote.objects.all(), message=already_voted)])
snippet = serializers.PrimaryKeyRelatedField(queryset=Snippet.objects.all())
def validate(self, data):
snippet = data.get('snippet')
voter = data.get('voter')
if voter==data['snippet'].owner:
raise serializers.ValidationError(u"Voter cannot vote for himself.")
return data
选民字段必须是只读的。问题是ReadOnlyField在数据结构中不可用。如何使用只读字段进行验证?
编辑:这是我的观点
class VoteViewSet(viewsets.ModelViewSet):
"""
This viewset automatically provides `list`, `create`, `retrieve`,
`update` and `destroy` actions.
"""
queryset = Vote.objects.all()
serializer_class = VoteSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,
IsOwnerOrReadOnly,)
def perform_create(self, serializer):
serializer.save(voter=self.request.user)
答案 0 :(得分:1)
在您的序列化程序中,您已经可以通过self.context['request'].user
访问当前用户:
所以你的代码应该是这样的:
if self.context['request'].user == data['snippet'].owner:
raise serializers.ValidationError(u"Voter cannot vote for himself.")
因此您可以移除perform_create
并使用此istead