django-filter return null queryset when wrong parameters

Viewed 284

I'm creating a simple API.

models.py

class TestModel(models.Model):
    tenor = models.IntegerField(db_column="tenor_field")
    currency = models.CharField(max_length=6, db_column="currency_field")
    value = models.FloatField(db_column="value_field")

serializer.py

class TestSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = TestModel
        fields = [ 'tenor', 'currency', 'value']

views.py

class TestView(viewsets.ReadOnlyModelViewSet):
    queryset = TestModel.objects.all()
    serializer_class = TestSerializer
    filterset_fields  = ['tenor','currency']

when I call

https://10.1.33.70/api/v1/base-rates/floating/?currency=**EUR**&tenor=**10**

I get

HTTP 200 OK
Allow: GET, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

[
    {
        "tenor": 10,
        "currency": "EUR",
        "value": 0.6
    }
]

and it is ok.

When I call https://10.1.33.70/api/v1/base-rates/floating/?currency=**EUR**&tenor=**xxx**

I get

HTTP 400 Bad Request
Allow: GET, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "tenor": [
        "Enter a number."
    ]
}

and it's also ok, because tenor is integer.

But when I call https://10.1.33.70/api/v1/base-rates/floating/?currency=**USD**&tenor=**10**

I get

HTTP 200 OK
Allow: GET, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

[]

because in database are only EUR position (not USD).

Is it possible to get a "HTTP 400 Bad Request" if I give a value to the "currency" parameter that is not in the database or can I define what values ​​can be given to the parameter and otherwise a "HTTP 400 Bad Request".

Thanks :)

1 Answers
Related