Models
class Task(Model):
employee_owner = ForeignKey(Employee, on_delete=CASCADE)
employee_doer = ForeignKey(Employee, on_delete=CASCADE)
Views
class TaskViewSet(ModelViewSet):
serializer_class = TaskSerializer
queryset = Task.objects.all()
filter_class = TaskFilter
Filter
class TaskFilter(FilterSet):
owner_id = NumberFilter(name='employee_owner__id')
doer_id = NumberFilter(name='employee_doer__id')
class Meta:
model = Task
fields = {
'owner_id',
'doer_id'
}
Endpoints
http://localhost:8000/api/tasks?owner_id=1&doer_id=1
(Gives only those tasks where owner and doer are the same employee)
http://localhost:8000/api/tasks?owner_id=1
(Gives only those tasks where owner is specific employee and doer is anyone)
http://localhost:8000/api/tasks?doer_id=1
(Gives only those tasks where doer is specific employee and owner is anyone)
What I want
I want an endpoint like:
http://localhost:8000/api/tasks?both_id=1
(Which would give me all results from above 3 endpoints)
I want django-filter to do filtering exactly like:
Task.objects.filter(
Q(employee_owner__id=1) | Q(employee_doer__id=1)
)
How can I achieve that ? Thank you.