'Django nested serializer get request shows value from an other model

I would like to create transactions with product quantities, I have this 3 models:

 class Products(models.Model):

    name = models.CharField(max_length=100)
    barcode = models.CharField(max_length=100)
    price = models.IntegerField(blank=True, default=1)
    quantity = models.IntegerField(blank=True)

    def __str__(self) -> str:
        return self.name

class ProductCount(models.Model):

    barcode = models.ForeignKey(
        Products, null=True, blank=True, on_delete=models.CASCADE)
    transaction = models.ForeignKey(
        Transactions, blank=True, on_delete=models.CASCADE)
    quantity = models.IntegerField(blank=True, default=1)

class Transactions(models.Model):

    staff = models.ForeignKey(
        Staff, blank=True, null=True, on_delete=models.CASCADE)
    services = models.ManyToManyField(
        Services, through='ServiceCount', related_name="transactions")
    products = models.ManyToManyField(
        Products, through='ProductCount', related_name="transactions")
    customer = models.ForeignKey(
        Customers, blank=True, null=True, on_delete=models.CASCADE)
    date = models.DateTimeField(auto_now_add=True)

Serializers:

class TransactionSerializer(serializers.ModelSerializer):
    products = ProductCountSerializer(many=True)
    services = ServiceCountSerializer(many=True)

    class Meta:
        model = Transactions
        fields = ('id', 'staff', 'products', 'services')

    def create(self, validated_data):
        product_data = validated_data.pop('products')
        validated_data.pop('services')
        transaction = Transactions.objects.create(**validated_data)
        for data in product_data:
            print(data)
            ProductCount.objects.create(transaction=transaction, **data)
        return transaction

class ProductCountSerializer(serializers.Serializer):
    quantity = serializers.IntegerField()
    barcode = serializers.PrimaryKeyRelatedField(
        queryset=Products.objects.all())
    # barcode = ProductSerializer()

    class Meta:
        model = ProductCount
        fields = ['barcode', 'quantity', 'transaction']
class ProductSerializer(serializers.ModelSerializer):

    class Meta:
        model = Products
        fields = ('name', 'price', 'quantity', 'barcode')

I do a post request on postman with this data:

{
    "staff": 1,
    "products": [{"barcode":1 , "quantity":5}, {"barcode":1 , "quantity":3}],
    "services": []
}

This works, but if I want to do a get request on transactions I get this back:

{
        "id": 1,
        "staff": 1,
        "products": [
            {
                "quantity": 10,
                "barcode": "1"
            },
            {
                "quantity": 10,
                "barcode": "1"
            }

On the ProductCount model, it saves the right quantity, but when I do the get request on the transactions model it shows the quantity of the product itself. Also on Django admin, I can not see the Products field on the Transactions model, but on the ProductCount model, I can see it related to the right transaction.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source