[Answer]-A room list of each apartment foreign key link

1👍

You should add a foreign key to the apartement into the room model:

class room(models.Model):
        apartement = models.ForeignKey(apartement)
        type = models.CharField(max_length=256)
        capacity = models.IntegerField()
        def __unicode__(self):
                return u'%s' % (self.type)

And then get a list of rooms:

apartements = apartement.objects.get(user=request.user)
rooms = apartements.room_set.all()

Template:

{% for r in rooms %}
    <td>{{ r.type }}</td>
    <td>{{ r.capacity }}</td>
{% endfor %}

EDIT: In case if you don’t need to get apartement instance you can get the same list of rooms with this code:

rooms = room.objects.filter(apartement__user=request.user)

Leave a comment