ForeignKey и ManyToManyField в списке всех объектов

Несмотря на изучение документации, я не могу найти решение. Я хочу показать в моих шаблонах значения, связанные с помощью ManyToManyField и ForeignKey.
. У меня есть такая модель:

class Adresse(models.Model):
    libelle = models.fields.CharField(max_length=100)
    numero = models.fields.IntegerField(null=True)
    street = models.fields.CharField(max_length=100)
    code = models.fields.IntegerField(null=True)
    city = models.fields.CharField(max_length=100, null=True)
    country = models.ForeignKey(Country, null=True, blank=True, on_delete=models.SET_NULL)
    def __str__(self):
        return f'{self.libelle}'

class Trip(models.Model):
    title = models.fields.CharField(max_length=100)
    traveller = models.ManyToManyField(Character)
    date_start = models.DateField(default=date.today)
    date_end = models.DateField(default=date.today)
    def __str__(self):
        return f'{self.title}'

class Activity(models.Model):
    trip = models.ForeignKey(Trip, null=True, blank=True, on_delete=models.SET_NULL)
    adresse = models.ForeignKey(Adresse, null=True, blank=True, on_delete=models.SET_NULL)
    date_start = models.DateField(default=date.today)
    date_end = models.DateField(default=date.today)
    class Meta:
        verbose_name_plural = "Activities"

и этот вид для отображения общего списка поездок:

from django.shortcuts import render

# Create your views here.

from django.contrib.auth.decorators import login_required

from django.shortcuts import get_object_or_404, redirect, render

from travel.models import Trip, Adresse, Activity

from profiles.models import Character

@login_required
def travel_list(request):
    travels = Trip.objects.all()
    travellers = Character.objects.all()
    template = 'travel/travel_list.html'
    context = {
        'travels' : travels,
        'travellers' : travellers,      
        }
    return render(request, template, context)

@login_required
def travel_detail(request, travel_id):
    travel = get_object_or_404(Trip, id=travel_id)
    activities = Activity.objects.filter(trip_id=travel_id)
    travellers = travel.traveller.all().order_by("last_name", "first_name")
    template = 'travel/travel_detail.html'
    context = {
        'travel': travel,
        'activities' : activities,
        'travellers' : travellers,
        }
    return render(request, template, context)

et ce template :

{% extends 'base.html' %}

{% load static %}

{% block content %}

<h2>Voyages</h2>

<table>

{% for travel in travels %}

<tr>

<td><a href="{% url 'travel-detail' travel.id %}">{{travel.title}}</a></td>

<td>{{travel.date_start}}</td>

<td>{{travel.date_end}}</td>

<td>
{% for traveller in travellers %}
{{traveller.last_name}}

{% endfor %}

</td>

</tr>

{% endfor %}

</table>

{% endblock %}

Я хотел бы отобразить таблицу с такой информацией:

| trip.title | trip.date_start | trip.date_end | traveller1.last_name, traveller2.last_name | activity1.adresse.city, activity2.adresse.city

Что такое хороший код?

Вернуться на верх