G
G
Gennady2018-08-28 11:39:48
Django
Gennady, 2018-08-28 11:39:48

How to make an ORM request?

there are these models:

class Team(models.Model):
    def __str__(self):
        return self.teamname
    teamname = models.CharField(max_length = 64, verbose_name='Название команды')
    member = models.ManyToManyField(User, verbose_name='Члены команды')

class Task(models.Model):
    def __str__(self):
        return self.tasktitle
    
    STATUS_CHOICE = (
        ("new", "Новый"),
        ("inprogress", "В процессе"),
        ("done", "Готово"),
    )

    TYPE_CHOICE = (
        ('nw', 'Новость'), 
        ('tk', 'Задача'), 
        ('nt', 'Уведомление') 
    )

    tasktitle = models.CharField(max_length = 128, verbose_name='Заголовок')
    description = models.TextField(max_length = 100000, verbose_name='Описание')
    datetoresolve = models.DateField(verbose_name='Срок выполнения')
    status = models.CharField(choices = STATUS_CHOICE, max_length = 10, blank=True, verbose_name='Статус')
    tasktype = models.CharField(choices = TYPE_CHOICE, max_length = 2, default='nt', verbose_name='Тип записи')
    
    resposiblePerson = models.ManyToManyField(User, blank=True, verbose_name='Ответственный сотрудник')
    responsibleTeam = models.ManyToManyField(Team, blank=True, verbose_name='Ответственная команда')
    
    watcher = models.ManyToManyField(User, blank=True, verbose_name='Наблюдатель', related_name='watcher_p')
    helper = models.ManyToManyField(User, blank=True, verbose_name='Помогающий', related_name='helper_p')
    performer =  models.ManyToManyField(User, blank=True, verbose_name='Исполнитель', related_name='peformer_p')

And now I need to generate a feed for each user, depending on whether he is in the team, whether the task is assigned to him or whether he is among the observers \ helping \ performers
I have no idea how to make such a request :(
I also understand that most likely the architecture solutions are initially
curve.Please tell me how to make a request in such cases (to determine whether the task applies to a person), or how best to change the model
Thank you in advance!
I forgot to add that I can get by a separate field, for example:
Task.objects.filter(resposiblePerson__username="gennady")

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2018-08-28
@genana40

from functools import reduce
import operator

from django.db.models import Q


user = request.user
filters = (
    Q(resposiblePerson=user),
    Q(responsibleTeam__member=user),
    Q(watcher=user),
    Q(helper=user),
    Q(performer=user),
)

tasks = Task.objects.filter(reduce(operator.or_, filters))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question