There are 3 models.

class Autor(models.Model): name = models.CharField(max_length=150, unique=True) description = models.CharField(max_length=250) class Book(models.Model): name = models.CharField(max_length=150, unique=True) country = models.CharField(max_length=50) class Genre(models.Model): name = models.CharField(max_length=150) autor = models.ForeignKey('Autor', on_delete=models.CASCADE) book = models.ForeignKey('Book', on_delete=models.CASCADE) 

There is such a test:

 assert response_data[1]['name'] == 'Horor' assert response_data[1]['book'] == 'first' assert response_data[1]['autor'] == 'Vova' assert response_data[1]['description'] == '' 

In views I do this:

 class CategoryView(DetailView): model = Autor template_name = '_' 

I understood it is necessary somehow through get_context_data (). And how then to turn in the template to the fields from different tables? Or do you need to link the table not by id, but by name?

    1 answer 1

    How then to turn in a template to fields from different tables?

    In your get_context_data override get_context_data and add a new key to the context:

     def get_context_data(self, **kwargs): data = super().get_context_data(**kwargs) data['books'] = Book.objects.all() return data 

    And this key becomes a variable in the template:

     {% for book in books %} <p>{{book.name}}<p> {% end for %} 

    As you can see from the example, through this variable you can access the fields of other tables.