How can I override Django Rest Framework viewset perform_create() method to set a default value to a field

I have a model called Product and two viewsets that manage the same model in different ways. Each one must set a different value for field product_type, which is set read_only in the serializer SuplementSerializer.

I trying to override perform_create() method and set the value for field product_type, but it always takes the default value.

This is my model:

class Product(models.Model):
    ROOM = 'ROOM'
    SUPLEMENT = 'SUPLEMENT'
    PRODUCT_TYPE_CHOICES = (
        (ROOM, _('Room')),
        (SUPLEMENT, _('Suplement'))
    )

    hotel = models.ForeignKey(Hotel, on_delete=models.PROTECT, related_name='products', verbose_name=_('Hotel'))
    name = models.CharField(max_length=100, verbose_name=_('Name'))
    product_type = models.CharField(max_length=9, choices=PRODUCT_TYPE_CHOICES, default=ROOM, verbose_name=_('Product Type'))
    room_type = models.ForeignKey(RoomType, null=True, blank=True, on_delete=models.PROTECT, related_name='products', verbose_name=_('Room Type'))
    plan_type = models.ForeignKey(PlanType, null=True, blank=True, on_delete=models.PROTECT, related_name='products', verbose_name=_('Plan Type'))
    content_type = models.ForeignKey(ContentType, null=True, blank=True, on_delete=models.PROTECT, related_name='rate_base_products', verbose_name=_('Rate Base'))
    object_id = models.PositiveIntegerField(null=True, blank=True)
    rate_base = GenericForeignKey('content_type', 'object_id')

    class Meta:
        verbose_name = _('Product')
        verbose_name_plural = _('Products')

    def __str__(self):
        return "[{}][{}]{}".format(self.id, self.hotel, self.name)

    def save(self, *vars, **kwargs):
        self.full_clean()
        return super().save(*vars, **kwargs)

    def clean(self, *vars, **kwargs):
        if self.content_type != None:
            if self.content_type.model != 'ages' and self.content_type.model != 'roombase' and self.content_type.model != 'product':
                raise CustomValidation(_("rate_base must be an instance of either 'Ages' or a 'RoomBase'"), 'rate_base', status.HTTP_400_BAD_REQUEST)

These are my viewsets:

class SuplementViewSet(viewsets.ModelViewSet):
    permission_classes = (permissions.IsAuthenticated,)
    queryset = models.Product.objects.filter(product_type=models.Product.SUPLEMENT)
    serializer_class = serializers.SuplementSerializer
    filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter,)
    search_fields = ('hotel__name', 'name')

    def perform_create(self, instance):
        instance.product_type = models.Product.SUPLEMENT
        instance.save()

class ProductViewSet(viewsets.ModelViewSet):
    permission_classes = (permissions.IsAuthenticated,)
    queryset = models.Product.objects.all()
    serializer_class = serializers.ProductSerializer
    filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter,)
    search_fields = ('hotel__name', 'name')
    filter_fields = ('hotel__name', 'name')

And this is my serializer:

class SuplementSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Product
        fields = ('id', 'hotel', 'product_type', 'name')
        read_only_fields = ('product_type',)

    def __init__(self, *args, **kwargs):
        exclude = kwargs.pop('exclude', None)

        super(SuplementSerializer, self).__init__(*args, **kwargs)

        if exclude is not None:
            for field_name in exclude:
                self.fields.pop(field_name)

I don't know why I can't set the value for field product_type.

1 answer