I have a project that is built using Django REST Framework. I have models Item
and Tag
, between which there's a many-to-many relationship. When requesting a list of Item
instances, I am using ModelMultipleChoiceFilter
to filter Item
list by tags.
Here's my filters.py
:
import django_filters
from .models import Item, Tag
class ItemTagFilter(django_filters.FilterSet):
tags = django_filters.ModelMultipleChoiceFilter(name='tags__text',
to_field_name='text',
queryset=Tag.objects.all(),
conjoined=False,)
class Meta:
model = Item
fields = ['tags']
As you may notice, since the value conjoined
is False
by default, I expect any Item
instance having any of the Tag
texts I request to be included in the resulting list. And it seems to be working for the existing Tag
instances recorded in the database.
The problem is, when I enter a non-existent Tag
text, an empty list is returned, even if I have sent several Tag
texts alongside it which do exist in the database. (i.e. I expect the filter to return the union of Item
elements which have any of the tags I send in my request)
I looked into the Django REST Framework documentation and several relevant SO posts like this one, but I could find neither the root cause of the issue, nor a solution to it. I'd appreciate any help.
You may find my models.py
and views.py
below, if you need further information.
models.py:
from django.db import models
class Tag(models.Model):
text = models.CharField(max_length = 100, unique = True)
...
class Item(models.Model):
info = models.CharField(max_length = 200)
tags = models.ManyToManyField(Tag, related_name='items')
views.py:
from rest_framework import generics
from .models import Item
from .filters import ItemTagFilter
import django_filters.rest_framework as filters
...
class ListCreateItemView(generics.ListCreateAPIView):
queryset = Item.objects.all()
filter_backends = (filters.DjangoFilterBackend,)
filter_class = ItemTagFilter
serializer_class = ItemSerializer