Django вложенный сериализатор get request показывает значение из другой модели

Я хочу создавать транзакции с количеством продуктов, у меня есть 3 модели:

 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")
    costumer = models.ForeignKey(
        Costumers, blank=True, null=True, on_delete=models.CASCADE)
    date = models.DateTimeField(auto_now_add=True)

Сериализаторы:

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

Я делаю запрос на почту на postman с такими данными:

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

Это работает, но если я хочу сделать запрос get для транзакций, я получаю следующее:

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

В модели ProductCount он сохраняет правильное количество, но когда я делаю запрос get в модели транзакций, он показывает количество самого продукта. Также в Django admin я не могу увидеть поле Products в модели Transactions, но в модели ProductCount я вижу его связанным с нужной транзакцией

Вернуться на верх