Opened 4 years ago

Closed 4 years ago

#31039 closed New feature (fixed)

Support __contained_by lookup for AutoFields and BigAutoFields.

Reported by: Bolay Michael Owned by: Hasan Ramezani
Component: contrib.postgres Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

AutoField can't use the __contained_by relation on postgres Range fields.

To support it, RangeContainedBy lookup class needs to add a map for serial data type and AutoField must register this lookup.

Example of unsupported feature

from django.contrib.postgres.fields import IntegerRangeField
from psycopg2.extras import NumericRange

class Model(models.Model):
    pass

class ModelBundle(models.Model):
   id_range = IntegerRangeField()

# this query is invalid
Model.objects.filter(id__contained_by=NumericRange(0, 10))

# and thus this one neither is
bundle = ModelBundle.object.get(...)
Model.objects.filter(id__contained_by=bundle.id_range)

Here is how I fix the missing feature

from django.contrib.postgres.fields.ranges import RangeContainedBy

class ExtendedRangeContainedBy(RangeContainedBy):
    type_mapping = {
        'serial': 'int4range',   # <---- add serial type mapping
        'integer': 'int4range',
        'bigint': 'int8range',
        'double precision': 'numrange',
        'date': 'daterange',
        'timestamp with time zone': 'tstzrange',
    }


AutoField.register_lookup(ExtendedRangeContainedBy)   # <---- register the lookup for AutoField

Change History (5)

comment:1 by Mariusz Felisiak, 4 years ago

Component: Uncategorizedcontrib.postgres
Summary: Allow `AutoField` to use `__contained_by` on postgress Range fieldsSupport __contained_by lookup for AutoFields and BigAutoFields.
Triage Stage: UnreviewedAccepted
Type: UncategorizedNew feature
Version: 2.2master

Agreed, we should support __contained_by lookup for AutoField's and BigAutoField's.

comment:2 by Mariusz Felisiak, 4 years ago

Easy pickings: set

comment:3 by Hasan Ramezani, 4 years ago

Owner: changed from nobody to Hasan Ramezani
Status: newassigned

comment:4 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

In 664521c5:

Refs #31039 -- Removed unnecessary registration of contained_by lookup for BigIntegerField.

It's already registered for IntegerField.

comment:5 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

Resolution: fixed
Status: assignedclosed

In 5d674eac:

Fixed #31039 -- Added support for contained_by lookup with AutoFields, SmallIntegerField, and DecimalField.

Note: See TracTickets for help on using tickets.
Back to Top