简体   繁体   中英

DRF use another model related fields in serializer

I have 3 model Product - Peyment - ProductDiscountControll

Peyment and ProductDiscountControll have relation to column " product " to Product table

I want to have related ProductDiscountControll data like discount and discount_code_precent in peyment serilizer at get request.

In quest to do that, I tried following code in my Serializer Class

def get_product_discount(self, obj):
    return obj.product.product_discount.discount

but server says :

Field name `product_discount` is not valid for model `Peyment`.

I also tried like this way:

product_discount = ProductDiscountControllSerializer(many=True,read_only=True)

but product_discount not available in result

my view is look like this

class PeymentAPIView(APIView, mixins.DestroyModelMixin):
    permission_classes = [IsSafeGuard]

    def get(self, request):
        pay = Peyment.objects.filter(
            email=request.user.email,
            status=0,

        )
        serializer = PeymentSerializer(instance=pay, many=True)
        return Response(serializer.data)

this is related Serializer class for get request:

class PeymentSerializer(ModelSerializer):
    producttitle = serializers.SerializerMethodField()

    def get_producttitle(self, obj):
        return obj.product.title

    productprice = serializers.SerializerMethodField()

    def get_productprice(self, obj):
        return obj.product.price


    def get_discount(self, obj):
        return obj.product_discount.discount
    #product_discount = ProductDiscountControllSerializer(many=True,read_only=True)


    class Meta:
        model = Peyment
        fields = [
            'product',
            'id',
            'producttitle',
            'productprice',
            'discount',
            'status',
            'user',
            'email',
            'transfer_id',
            'created_date',
            'updated_date',
        ]
        read_only_fields = ['email', 'user', 'producttitle', 'productprice']

this is Product model:

class Product(models.Model):
    product_id = models.AutoField(primary_key=True)
    author = models.ForeignKey(User, on_delete=models.CASCADE, db_index=True)
    title = models.CharField(max_length=200)
    video_length = models.CharField(max_length=20, null=True, blank=True)
    mini_description = models.CharField(max_length=1000, null=True, blank=True)
    full_description = models.TextField(null=True, blank=True)
    you_need = models.CharField(max_length=1000, null=True)
    you_learn = models.CharField(max_length=2000, null=True)
    price = models.CharField(max_length=50, null=True, blank=True)
    video_level = models.CharField(max_length=100, null=True, blank=True)
    created_date = models.DateTimeField(auto_now_add=True)
    updated_date = models.DateTimeField(auto_now=True)
    image = models.FileField(upload_to=upload_to_custom_p,null=True,blank=True)

Peyment model:

class Peyment(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE, to_field='product_id',
                                related_name='product_peyment')
    status = models.CharField(max_length=30, null=True)
    user = models.ForeignKey(User, on_delete=models.DO_NOTHING)
    transfer_id = models.CharField(max_length=100, null=True, blank=True)
    email = models.EmailField()
    created_date = models.DateTimeField(auto_now_add=True)
    updated_date = models.DateTimeField(auto_now=True)

and discount model:

class ProductDiscountControll(models.Model):
    product = models.OneToOneField(Product, on_delete=models.CASCADE, to_field='product_id',
                                related_name='product_discount')
    discount = models.IntegerField(max_length=50, null=True, blank=True)
    discount_code = models.CharField(max_length=50, null=True, blank=True)
    discount_code_precent = models.CharField(max_length=80, null=True, blank=True)
    updated_date = models.DateTimeField(auto_now=True)

updated :

# product peyment
class PeymentSerializer(ModelSerializer):
    producttitle = serializers.SerializerMethodField()

    def get_producttitle(self, obj):
        return obj.product.title

    productprice = serializers.SerializerMethodField()

    def get_productprice(self, obj):
        return obj.product.price

    def get_discount(self, obj):
        serializer = ProductDiscountControllSerializer(obj.product.product_discount)
        return serializer.data


    class Meta:
        model = Peyment
        fields = [
            'product',
            'id',
            'producttitle',
            'productprice',
            'discount',
            'status',
            'user',
            'email',
            'transfer_id',
            'created_date',
            'updated_date',
        ]
        read_only_fields = ['email', 'user', 'producttitle', 'productprice']

You can just use product.product_discount field name in serializer's method. To return serialized data you should pass this value to ProductDiscountControllSerializer and return serializer.data :

def get_discount(self, obj):
    discount = getattr(obj.product, 'product_discount', None)
    if discount: 
        serializer = ProductDiscountControllSerializer(discount)
        return serializer.data 
    return None

UPD

You should explicitly declare discount field in serializer with SerializerMethodField to use it in fileds list:

class PeymentSerializer(ModelSerializer):
    discount = serializers.SerializerMethodField()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM