[Answer]-Django: Make a query inside a class

1👍

I don’t know what you mean by “inside a class”.

You can make your query like this:

Person.objects.filter(
    is_doctor=False,
    contract__type_contract='full',
    contract__starting_date__gte=start_date,
    contract__ending_date__lte=end_date
)

If you want to define a method to execute this query, that is normally done inside a model manager:

class PersonManager(models.Manager):
    def full_time_no_doctors_contract_between(self, start_date, end_date):
        return self.filter(...)


class Person(models.Model):
    ...
    objects = PersonManager()

and now you can do:

Person.objects.full_time_no_doctors_contract_between(start_date, end_date)

Leave a comment