Django REST Framework : “This field is required.” with required=False and unique_together

妖精的绣舞 提交于 2019-12-22 04:57:06

问题


I want to save a simple model with Django REST Framework. The only requirement is that UserVote.created_by is set automatically within the perform_create() method. This fails with this exception:

{
    "created_by": [
        "This field is required."
    ]
}

I guess it is because of the unique_together index.

models.py:

class UserVote(models.Model):
    created_by = models.ForeignKey(User, related_name='uservotes')
    rating = models.ForeignKey(Rating)

    class Meta:
        unique_together = ('created_by', 'rating')

serializers.py

class UserVoteSerializer(serializers.ModelSerializer):
    id = serializers.IntegerField(read_only=True)
    created_by = UserSerializer(read_only=True)

    class Meta:
        model = UserVote
        fields = ('id', 'rating', 'created_by')

views.py

class UserVoteViewSet(viewsets.ModelViewSet):
    queryset = UserVote.objects.all()
    serializer_class = UserVoteSerializer
    permission_classes = (IsCreatedByOrReadOnly, )

    def perform_create(self, serializer):
        serializer.save(created_by=self.request.user)

How can I save my model in DRF without having the user to supply created_by and instead set this field automatically in code?

Thanks in advance!


回答1:


I had a similar problem and I solved it by explicitly creating and passing a new instance to the serializer. In the UserVoteViewSet you have to substitute perform_create with create:

 def create(self, request, *args, **kwargs):
    uv = UserVote(created_by=self.request.user)
    serializer = self.serializer_class(uv, data=request.data)
    if serializer.is_valid():
        serializer.save()
        return Response(serializer.data, status=status.HTTP_201_CREATED)
    else:
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)



回答2:


The other weird way you can do is use signals like this

@receiver(pre_save, sender=UserVote)
def intercept_UserVote(sender, instance, *args, **kwargs):
    import inspect
    for frame_record in inspect.stack():
        if frame_record[3]=='get_response':
            request = frame_record[0].f_locals['request']
            break
    else:
        request = None

    instance.pre_save(request)

Then basically you can define pre_save in your model

def pre_save(self, request):
    # do some other stuff
    # Although it shouldn't happen but handle the case if request is None
    self.created_by = request.user

The advantage of this system is you can use same bit of code for every model. If you need to change anything just change in pre_save(). You can add more stuff as well




回答3:


I was able to solve this with one-liner in views.py

def create(self, request, *args, **kwargs):
    request.data.update({'created_by': request.user.id})
    return super(UserVoteViewSet, self).create(request, *args, **kwargs)

Since this view expects user to be authenticated, don't forget to extend permission_classes for rest_framework.permissions.IsAuthenticated




回答4:


Below code worked for me.

Even I was facing same error after many experiments found something, so added all fields in serializer.py in class meta, as shown below -

class Emp_UniSerializer( serializers.ModelSerializer ):
    class Meta:
        model = table
        fields = '__all__'  # To fetch For All Fields
        extra_kwargs = {'std_code': {'required': False},'uni_code': {'required': False},'last_name': {'required': False},'first_name': {'required': False}}

Here, we can update any field which are in "extra_kwargs", it wont show error ["This field is required."]



来源:https://stackoverflow.com/questions/34661853/django-rest-framework-this-field-is-required-with-required-false-and-unique

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!