Error when trying to Serialize a ManyToMany relationship (Django with DRF)

I'm getting an error when I try to serialize a many-to-many relationship

The error description that is shown to me in the console is this:

AttributeError: Got AttributeError when attempting to get a value for field product on serializer Ped_ProDetail. The serializer field might be named incorrectly and not match any attribute or key on the Product instance. Original exception text was: 'Product' object has no attribute 'product'.

The models involved in the relationship are written like this:

class Produto(models.Model):
    valor_unitario = models.DecimalField(max_digits=10, decimal_places=2, default=0.00)
    nome = models.CharField(max_length=75)
    descricao = models.TextField()
    genero = models.CharField(max_length=10, default="Indefinido")
    qtd_estoque = models.IntegerField()
    cor = models.ForeignKey(Cor, on_delete=models.PROTECT, related_name="produtos")
    tamanho = models.ForeignKey(
        Tamanho, on_delete=models.PROTECT, related_name="produtos"
    marca = models.ForeignKey(Marca, on_delete=models.PROTECT, related_name="produtos")

class Pedido(models.Model):
    endereco_entrega = models.ForeignKey(
        Endereco, on_delete=models.PROTECT, null=True, related_name="pedidos"
    forma_pagamento = models.ForeignKey(
        Forma_Pagamento, on_delete=models.PROTECT, null=True, related_name="pedidos"
    usuario_dono = models.ForeignKey(
        get_user_model(), on_delete=models.PROTECT, related_name="pedidos"
    data_entrega = models.DateField()
    data_pedido = models.DateField(
    finalizado = models.BooleanField(default=False)
    qtd_parcela = models.IntegerField()
    valor_parcela = models.DecimalField(max_digits=10, decimal_places=2, default=0.00)
    preco_total = models.DecimalField(max_digits=10, decimal_places=2, default=0.00)
    itens = models.ManyToManyField(Produto, related_name="pedidos", through="Ped_Pro")

class Ped_Pro(models.Model):
    produto = models.ForeignKey(
        Produto, on_delete=models.PROTECT, related_name="ped_pros"
    pedido = models.ForeignKey(
        Pedido, on_delete=models.PROTECT, related_name="ped_pros"
    qtd_produto = models.IntegerField(default=1)
    data_entrada = models.DateTimeField(

The serializers:

class ProdutoSerializer(ModelSerializer):
    class Meta:
        model = Produto
        fields = "__all__"

class Ped_ProSerializer(ModelSerializer):
    class Meta:
        model = Ped_Pro
        fields = "__all__"

class PedidoSerializer(ModelSerializer):
    itens = Ped_ProSerializer(many=True, read_only=True)

    class Meta:
        model = Pedido
        fields = ("usuario_dono", "data_pedido", "data_entrega", "itens")

Could you help me find a way to the solution?

Project link on Github

In class ped pro you have 2 times the same related name: ped_pros

Back to Top