D
D
Dmitry Kuksenok2020-05-19 12:04:51
Django
Dmitry Kuksenok, 2020-05-19 12:04:51

Django ORM: filtering users not on vacation [request creation]?

Good afternoon!
I just can't figure out how to make a request using Django ORM, I'm new to django.

env

Django 3
Python 3.8
PostgreSQL 12


The following models are available (simplified): Employee, Vacation, Vacation Feedback:
class Employee(models.Model):
    username = models.SlugField(unique=True)

class Vacation(models.Model):
    employee = models.ForeignKey(Employee, on_delete=models.CASCADE)
    start_date = models.DateField()
    end_date = models.DataField()

class VacationReview(models.Model):
    employee = models.ForeignKey(Employee, on_delete=models.CASCADE)
    start_date = models.DateField()
    end_date = models.DataField()


You need to get a list of employees who are not on vacation.
The condition is obtained:
= no vacation records for employee
or ( ( date.now() < vacation.start or date.now() > vacation.end ) and no feedback records )
or ( there are feedback records and ( date.now() >= feedback .start and date.now() <= callback.end ) )


I would be grateful if you provide links to information for a better understanding of Django ORM in general and complex queries in particular.
Thanks in advance!

decision

Спасибо за помощь @bacon
class EmployeesAtWorkView(LoginRequiredMixin, generic.ListView):
    model = Employee
    template_name = 'registry/employee_list.html'

    def get_queryset(self):
        date = timezone.now()
        not_dismissal = Q(dismissal_date__isnull=True) | Q(dismissal_date__gt=date)
        not_vacation = Q(vacation__isnull=True) | (Q(vacation__start_date__gt=date) | Q(vacation__end_date__lt=date))
        is_vacation_review = Q(vacationreview__isnull=False) & (Q(vacationreview__start_date__lte=date) & Q(vacationreview__end_date__gte=date))
        return Employee.objects.filter(
            not_dismissal &
            ( not_vacation |
            is_vacation_review )
        )

class EmployeesVacationView(LoginRequiredMixin, generic.ListView):
    model = Employee
    template_name = 'registry/employee_list.html'

    def get_queryset(self):
        date = timezone.now()
        not_dismissal = Q(dismissal_date__isnull=True) | Q(dismissal_date__gt=date)
        is_vacation = Q(vacation__isnull=False) | (Q(vacation__start_date__lte=date) & Q(vacation__end_date__gte=date))
        not_vacation_review = Q(vacationreview__isnull=True) | (Q(vacationreview__start_date__gt=date) | Q(vacationreview__end_date__lt=date))
        return Employee.objects.filter(
            not_dismissal &
            is_vacation &
            not_vacation_review
        )

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-05-19
@DKuksenok

https://docs.djangoproject.com/en/3.0/topics/db/qu...
https://docs.djangoproject.com/en/3.0/ref/models/q...
https://docs. djangoproject.com/en/3.0/ref/models/q...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question