E
E
Evgeny Abaev2020-04-29 21:34:04
Django
Evgeny Abaev, 2020-04-29 21:34:04

How to link webform and entity queries from database in Django?

There are classes: subject and project.
The subject class is associated with the ForeignKey project. There is a page with a list of items:

spoiler
5ea9c73c855d9233992332.jpeg

Tell me how, when you click on the block with the name of the subject, display all projects related to this subject.

spoiler
models.py
class Project(models.Model):
    """Проект"""
    title = models.CharField("Название", max_length=200)
    description = models.TextField("Описание")
    lesson = models.ForeignKey(Lesson, verbose_name="предметы", on_delete=models.SET_NULL, null=True)
    student = models.ManyToManyField(Student, verbose_name="ученики(цы)", related_name="student_project")
    create_date = models.DateField("Дата создания", default=now())
    url = models.SlugField(max_length=160, unique=True)
    done = models.BooleanField("Завершённый", default=False)
    poster = models.ImageField("Постер", upload_to="student_projects/")

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse("project_detail", kwargs={"slug": self.url})

    class Meta:
        verbose_name = "Проект"
        verbose_name_plural = "Проекты"

class Lesson(models.Model):
    """Предмет"""
    title = models.CharField("Название", max_length=150)
    image = models.ImageField("Изображение", upload_to="lessons/")
    url = models.SlugField(max_length=160, unique=True)

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = "Предмет"
        verbose_name_plural = "Предметы"


views.py
class LessonsView(ListView):
    """Список предметов"""
    model = Lesson
    queryset = Lesson.objects.all()


class ProjectsView(ListView):
    """Список проектов"""
    model = Project
    queryset = Project.objects.all() #lesson__title="Математика"


lesson_list.html
{% for lesson in lesson_list %}
                    <form>
                        <div class="col-md-4">
                            <a href=" ">
                                <div class="card mb-4 shadow-lg rounded">
                                    <!--<div class="men-thumb-item">
                                           <img src="{{ lesson.image.url }}" class="img-fluid" alt="" >
                                       </div>-->

                                    <div class="men-thumb-item" style="width: 500px; height: 250px; border-width: 2px ;border-color: #a30f1d">
                                        <p style="text-decoration: none; color: #721c24">{{ lesson.title }}</p>
                                    </div>
                                </div>
                            </a>
                        </div>
                    </form>
                {% endfor %}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sanya Hihi Haha, 2020-04-29
@ValarMayar

select_related()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question