Z
Z
zyivan3562021-06-09 23:03:25
Django
zyivan356, 2021-06-09 23:03:25

How to output objects in Django with filtering if ManyToMany matches?

There is a problem in the implementation of the code.

class Course(models.Model):
    title = models.CharField("Название курса", max_length=36)
    description = models.TextField("Описание курса", max_length=60)
    author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='author')
    pubdate = models.DateTimeField("Время создания")
    preview = models.ImageField(upload_to='media/preview/')
    duration = models.PositiveIntegerField("Продолжительность курса в часах", validators=[MaxValueValidator(999)])
    amount = models.PositiveIntegerField("Количество лекций в курсе", validators=[MaxValueValidator(999)])
    category = models.ForeignKey(Category, verbose_name="Категория", on_delete=models.CASCADE)
    price = models.PositiveIntegerField("Цена", validators=[MaxValueValidator(99999)])
    lessons = models.ManyToManyField(Lesson, verbose_name="Уроки", blank=True)
    owners = models.ManyToManyField(settings.AUTH_USER_MODEL, verbose_name="Владельцы", blank=True, related_name='owners')


The goal is this: to make a page for each user where all purchased courses will be stored. Such users who bought the course are called "Owners" (owners).
I can't make the function of displaying and filtering so that courses are on these pages only for those who bought them.
Thanks in advance for your help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
zyivan356, 2021-06-09
@zyivan356

Decided by hand!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question