具有OneToOne关系的模型的DRF序列化程序

时间:2017-05-30 09:54:25

标签: django django-rest-framework

鉴于具有OneToOne关系的Django模型,如何设置Django Rest Framework Serializers&观点,以便API消费者支付现金和支付支付的终点包括支付模式并允许所有CRUD功能?

我尝试过关注this SO Question here,但似乎无法复制功能。

模型

class Payment(models.Model):
    """
    Payment Log
    """
    merchant = models.ForeignKey(Entity, on_delete=models.CASCADE, related_name="%(app_label)s_%(class)s_merchant")
    customer = models.ForeignKey(Entity, on_delete=models.CASCADE, related_name="%(app_label)s_%(class)s_customer")
    payment_method = models.ForeignKey(PaymentMethod, on_delete=models.CASCADE)
    trx_amount = models.DecimalField(max_digits=10, decimal_places=3)

class PaymentCash(models.Model):
    """
    Cash payments
    """
    payment = models.OneToOneField(Payment, on_delete=models.CASCADE, primary_key=True)
    date_paid = models.DateField()
    # Other fields

class PaymentCheque(models.Model):
    """
    Cheque deposits
    """
    payment = models.OneToOneField(Payment, on_delete=models.CASCADE, primary_key=True)
    cheque_number = models.CharField(max_length=50)
    # Other fields

示例视图

class PaymentCashViewSet(ListCreateRetrieveUpdateViewSet):
    """
    Cash payment view
    """
    queryset = PaymentCash.objects.all()
    serializer_class = PaymentCashSerializer
    permission_classes = (IsAuthenticated, HasPermission)

当前序列化程序 这虽然没有使用付款模式中的所有字段。

class PaymentCashSerializer(serializers.ModelSerializer):
    """Serialize model"""
    class Meta:
        model = PaymentCash
        fields = ('payment', 'date_paid')

使用:Django == 1.10.2& djangorestframework == 3.5.1

1 个答案:

答案 0 :(得分:1)

鉴于您的模型和视图,我碰巧为您的视图编写了一些序列化程序。可能对你没什么帮助,因为我对你的项目信息有限。

但是,就我所知,到目前为止,我碰巧为你编写序列化器,

class EntitySerializer(serializers.ModelSerializer):

    class Meta:
        model = Entity
        fields = '__all__'

class PaymentMethodSerializer(serializers.ModelSerializer):

    class Meta:
        model = PaymentMethod
        fields = '__all__'

class PaymentSerializer(serializers.ModelSerializer):
    customer = EntitySerializer()
    merchant = EntitySerializer()
    payment_method = PaymentMethodSerializer()

    class Meta:
        model = Payment
        fields = ('id', 'payment_method', 'trx_amount', 'customer', 'merchant')

class PaymentCashSerializer(serializers.ModelSerializer):
    """Serialize model"""
    payment = PaymentSerializer()
    class Meta:
        model = PaymentCash
        fields = ('payment', 'date_paid')

    def create(self, validated_data):
        payment_data = validated_data.pop('payment')
        customer_data = payment_data.pop('customer')
        merchant_data = payment_data.pop('merchant')
        payment_method_data = payment_data.pop('payment_method')
        customer, c = Entity.objects.get_or_create(**customer_data)
        merchant, m = Entity.objects.get_or_create(**merchant_data)
        payment_method, p = PaymentMethod.objects.get_or_create(**payment_method_data)
        payment = Payment.objects.create(customer=customer, merchant=merchant, payment_method=payment_method, **payment_data)
        instance  = PaymentCash.objects.create(payment=payment, **validated_data)
        return instance

    def update(self, validated_data):
        payment_data = validated_data.pop('payment')
        customer_data = payment_data.pop('customer')
        merchant_data = payment_data.pop('merchant')
        payment_method_data = payment_data.pop('payment_method')
        customer, c = Entity.objects.get(**customer_data)
        merchant, m = Entity.objects.get(**merchant_data)
        payment_method, p = PaymentMethod.objects.get(**payment_method_data)
        payment = Payment.objects.get(customer=customer, merchant=merchant, payment_method=payment_method, **payment_data)
        instance  = PaymentCash.objects.get(id=validated_data.get('id'))
        instance.payment = payment
        instance.save()
        return instance

自定义创建和更新方法需要您进行一些修改,因为我只为理想的场景编写过。希望这对你有很大的帮助。