Django n:n relationship with grid call

Asked

Viewed 98 times

0

Good night.

I am creating a system in Python/Django where I have a relationship n:n between a table Person and another Process and for this I created a third entity called Procedural part.

I’ve done the following:

class Pessoa(models.Model):
    nome            = models.CharField(max_length=255)
    data_nascimento = models.DateField(null=True, blank=True)
    [...]

    def __str__(self):
        return self.nome


class Processo(models.Model):
    numero              = models.CharField('número', max_length=25)
    prioridade          = models.CharField(max_length=20)
    [...]
    pessoas             = models.ManyToManyField(Pessoa, through='ParteProcessual', related_name='parte_processual_mtm', blank=True)

    def __str__(self):
        return self.numero


class ParteProcessual(models.Model):
    pessoa = models.ForeignKey(Pessoa, on_delete=models.PROTECT, related_name='pessoa_fk')
    processo = models.ForeignKey(Processo, on_delete=models.CASCADE, related_name='processo_fk')
    polo_processual = models.CharField(max_length=20, choices=POLO_PROCESSUAL)
    tipo_parte = models.CharField('tipo de parte', max_length=20) #Autor, réu, embargante, impungnante, etc.
    is_cliente = models.BooleanField('cliente')

    def __str__(self):
        return self.pessoa.nome +' - Processo n.º '+ self.processo.numero

In Admin is working ok the relationship, but in HTML I made a list of processes and I’m trying to get the name of people (separated by comma) by the relationship people constant in the model Process and I’m not getting it.

In the template I did the following:

{% for processo in processos %}
     <tr role="row" class="odd">
         <td class="sorting_1">{{ processo.data_ajuizamento|date:"d/m/Y" }}</td>
         <td>{{ processo.numero }}</td>
         <td>{{ processo.fase_processual }}</td>
         <td>{{ processo.pessoas }}</td>
         <td>{{ processo.status }}</td>
         <td class="p-0"><a href="{% url 'edicao-processo' processo.id %}" class="btn btn-warning"><i class="fas fa-edit"></i></a></td>
         <td class="p-0"><a href="#modalDelete" data-toggle="modal" data-id="{{ processo.id }}" class="modalBtn btn btn-danger" ><i class="fas fa-eraser"></i></a></td>
     </tr>
{% endfor %}

In the listing the following error appears: people.People.None. Someone knows the solution?

1 answer

1

Hello, you should iterate on the attribute pessoas. Something like, {% for pessoa in processo.pessoas.all %}. Then within each iteration object you will be able to access the class attributes Pessoa. Now, if you want to access the class attributes ParteProcessual you must do as follows: {% for obj in processo.processo_fk.all %}, from obj you can access the attributes polo_processual, tipo_parte and is_cliente.

  • Thank you, friend. After a lot of cracking my head I have achieved just this way that you explained.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.