How to list two related models

1

I have these two models

class Comunidad(models.Model):
rut = models.IntegerField(primary_key=True)
nombre = models.CharField(max_length=50)
email = models.CharField(max_length=50)
direccion = models.CharField(max_length=70)

 def __str__(self):
    return str(self.rut)

 class Meta:
    managed = False
    db_table = 'comunidad'

class Asiste(models.Model):
id_capacitacion = models.ForeignKey('Capacitacion', models.DO_NOTHING, db_column='id_capacitacion', primary_key=True)
rut = models.ForeignKey('Comunidad', models.DO_NOTHING, db_column='rut')
fecha = models.DateField()

 class Meta:
    managed = False
    db_table = 'asiste'
    unique_together = (('id_capacitacion', 'rut'),)

and in a listview I want to list all of it in this case the id the rut and date but also I want next I see the name associated with the rut that obviously this name I find in my model 'Community', this is my listview

class persona_lista(ListView):
model = Asiste
template_name = 'asistencia.html'

and my template would be this way

 {% for persona in object_list %}
    <tr>
        <th scope="row">{{persona.id_capacitacion }}</th>
        <td>{{persona.rut }}</td>
        <td>{{persona.fecha }}</td>
    </tr>
    {%endfor%}

I do not know how to add the column of the name associated with the rut, rut would be the id of each person.

    
asked by Slasher 30.11.2018 в 00:52
source

1 answer

0

You can read this documentation of Django which is about relating models.

For your case it would be something like this:

class Comunidad(models.Model):
   pass

class Asiste(..):
   rut = Comunidad()

Then, given the object, you would do:

c = Comunidad.objects.all()
a = c.asiste_set.all()

The% co_of% object is available when related_nameno specify

    
answered by 30.11.2018 в 16:50