如何覆盖Django Rest Framework viewset perform_create()方法以将默认值设置为字段

时间:2018-07-11 03:32:40

标签: python django django-rest-framework

我有一个名为Product的模型和两个以不同方式管理同一模型的视图集。每个人都必须为字段product_type设置一个不同的值,该值在串行器SuplementSerializer中设置为只读。

我试图覆盖perform_create()方法并为字段product_type设置值,但是它始终采用默认值。

这是我的模特

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)

这些是我的视图集:

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')

这是我的序列化器:

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)

我不知道为什么不能为字段product_type设置值。

1 个答案:

答案 0 :(得分:0)

perform_create()方法的参数是序列化程序实例,而不是对象实例。您可以将字段设置如下

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

在此处查看说明http://www.django-rest-framework.org/tutorial/6-viewsets-and-routers/#refactoring-to-use-viewsets