WitnessTruth
WitnessTruth

Reputation: 579

Django - Direct assignment to the forward side of a many-to-many set is prohibited

I have this class in my project:

class ManejoEventoSanitario(models.Model):
   id_evento_sanitario = models.AutoField(primary_key=True)
   id_tipo_evento = models.ForeignKey(TipoEvento, on_delete=models.PROTECT)   
   descricao = models.CharField(max_length=90, blank=True, null=True)
   dt_evento_sanitario = models.DateField(blank=True, null=True)
   dt_prox_evento = models.DateField(blank=True, null=True)
   responsavel = models.ForeignKey(Pessoa, on_delete=models.PROTECT)   
   animais = models.ManyToManyField(Animal)
   produtos = models.ManyToManyField(DoseProduto)

   objects = models.Manager()

And I have this post endpoint in my ._views :

class ApiEventoSanitarioAnimal(APIView):
def post(self, request, format=None):
    data_evento = request.POST['data_evento']
    tipo_evento = request.POST['tipo_evento']
    responsavel = request.POST['responsavel']
    descricao = request.POST['descricao']
    data_proximo_evento = request.POST['data_proximo_evento']
    animais_json = jsonpickle.decode(request.POST['animais'])
    produtos_json = jsonpickle.decode(request.POST['produtos'])

    tipo_evento = get_object_or_404(TipoEvento, id_tipo_evento=tipo_evento)
    responsavel = get_object_or_404(Pessoa, id_pessoa=responsavel)

    animais = set()
    for animal_json_id in animais_json:
        animal = get_object_or_404(Animal, id_animal=animal_json_id)
        animais.add(animal)

    dose_quantidade_produtos = set()
    for produto_json in produtos_json:
        produto = get_object_or_404(Produto, id_produto=produto_json['id'])
        unidade_medida = Unidade.objects.filter(descricao=produto_json['unidade']).first()
        dose_quantidade = produto_json['dose']

        dose_produto = DoseProduto()
        dose_produto.id_produto = produto
        dose_produto.id_unidade_medida = unidade_medida
        dose_produto.quantidade = dose_quantidade
        dose_produto.save()
        dose_quantidade_produtos.add(dose_produto)

    m_evento_sanitario = ManejoEventoSanitario()
    m_evento_sanitario.dt_evento_sanitario = data_evento
    m_evento_sanitario.id_tipo_evento = tipo_evento
    m_evento_sanitario.responsavel = responsavel
    m_evento_sanitario.descricao = descricao
    m_evento_sanitario.save()

    #Adiciono os animais e produtos ao evento sanitario cadastro
    m_evento_sanitario.animais = animais
    m_evento_sanitario.produtos = dose_quantidade_produtos
    m_evento_sanitario.save()

    return Response(status=status.HTTP_200_OK)

What I'm trying to do is:

But when the code arrives in this part:

m_evento_sanitario.animais = animais
m_evento_sanitario.produtos = dose_quantidade_produtos
m_evento_sanitario.save()

It throws this error:

TypeError: Direct assignment to the forward side of a many-to-many set is prohibited. Use animais.set() instead.

I tried as a list too but it doesn't work. The animais and dose_quantidades_produtos already are a set.

Please, what am I doing wrong?

Upvotes: 1

Views: 2105

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599638

There's no reason to put those into a set. Move the creation of m_evento_sanitario before those loops, then for each one instead of adding it to a set add it directly to the m2m:

for animal_json_id in animais_json:
    animal = get_object_or_404(Animal, id_animal=animal_json_id)
    m_evento_sanitario.animais.add(animal)

and similarly for produtos.

Upvotes: 2

Related Questions