'Django filter based on SerializerMethodField
I have a similar type of code provided below.
serializer.py
class FooSerailizer(BaseModelSerializer):
value = serializers.SerializerMethodField(read_only=True)
def get_value(self,obj):
# some statement of code
# return statement
class meta:
model = Foo
fields = '__all__'
filters.py
class FooFilter(filters.FilterSet):
class meta:
model = models.Foo
fields = '__all__'
The API request is http://127.0.0.1:8000/api/?value=some_value.
The response I am getting when I am calling the above API:
{
"value": [
"Select a valid choice. That choice is not one of the available choices."
]
}
How to get the JSON response correctly?
Solution 1:[1]
Django cannot filter based on serializer, you will need to create a custom filter field in your filterset. Check out https://django-filter.readthedocs.io/en/stable/ref/filters.html.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ashin Shakya |
