Hipo / drf-extra-fields

Extra Fields for Django Rest Framework
Apache License 2.0
670 stars 91 forks source link

Using NumericRange with IntegerRangeField generates invalid SQL with latest psycopg #209

Open vbabiy opened 3 weeks ago

vbabiy commented 3 weeks ago

If you have a serializer like this with a IntegerRangeField

class StuffSerializer(serializers.ModelSerializer):
    numbers = IntegerRangeField(
        default=NumericRange(lower=0, upper=86400),
        required=False,
        allow_null=True,
    )

At creation time it will create a query that has a double cast '(,)'::numrange::int4range

This happens because NumericRange is casted by the psycopq transformer.

vbabiy commented 3 weeks ago

You can work around this bug by defining custom fields:

class MyIntegerRangeField(IntegerRangeField):
    range_type = Int4Range

class StuffSerializer(serializers.ModelSerializer):
    numbers = MyIntegerRangeField(
        default=Int4Range(lower=0, upper=86400),
        required=False,
        allow_null=True,
    )