Adnan
Adnan

Reputation: 182

Django Rest Framework: make a serializer field not required in post data when perform create re

I have the following setup:

Models.py

class QuoteModel(models.Model):
    """
        this model stores initial information for the Quote
    """
    quote_number = models.CharField(max_length=20,
                                    unique=True,
                                    help_text="Please use the quote number from pms",)
    description = models.CharField(max_length=200,
                                   help_text="Enter description that you might find helpful")
    creator = models.ForeignKey(User)
    date_created = models.DateField(auto_now_add=True)

Serializer.py

# Serializers define the API representation.
class QuoteModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = Quote
        fields = ('id', 'quote_number', 'description', 'creator', 'date_created')
        read_only_fields = ('date_created',)

View.py

class QuoteListCreateView(generics.ListCreateAPIView):
    queryset = Quote.objects.all()
    serializer_class = QuoteModelSerializer
    permission_classes = (permissions.IsAuthenticated, )

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

In the snippet, I am setting up the creator field of QuoteModel in the view's perform_create method. Since QuoteListCreateView is an authenticated only view, therefore, I don't want to pass the creator during the quote create request (post to /quotes). creator is a part of the serializer class, the post request returns the following if I don't pass the creator. "creator": [ "This field is required." ]"

Therefore, my question is- Is there anyway to make the creator parameter optional on post request? I can not add null=true on creator field in model.py because it is required from models perspective.

Thanks in advance.

Fix

overriding the creator attribute in the model serializer and set required to False resolved the issue.

class QuoteModelSerializer(serializers.ModelSerializer):
    creator = serializers.PrimaryKeyRelatedField(required=False, queryset=User.objects.all())
    class Meta:
        model = Quote
        fields = ('id', 'quote_number', 'description', 'creator', 'date_created')
        read_only_fields = ('date_created',)

Upvotes: 4

Views: 6157

Answers (1)

kekkler
kekkler

Reputation: 326

Based on your description of the problem. Specifically:

I don't want to pass the creator during the quote create request.

It sounds like you want the creator field to be read-only, yet be populated on create. If this is the case, I suggest you use the following:

class QuoteModelSerializer(serializers.ModelSerializer):
    creator = serializers.PrimaryKeyRelatedField(
        read_only=True,
        default=serializers.CurrentUserDefault()
    )
    class Meta:
        model = Quote
        fields = ('id', 'quote_number', 'description', 'creator', 'date_created')
        read_only_fields = ('date_created',)

Upvotes: 8

Related Questions