2

I want to get the upper bound of a RangeField via django queryset.

When you have access to the python object, my_model.size_range.upper works. Not inside the query engine, not working with F, When, Case...

class MyModel(models.Model):
    size_range = IntergerRangeField()

MyModel.objects.all().annotate(new_upper=F('size_range__upper') + 1)
FieldError: Unsupported lookup 'upper' for IntegerRangeField or join on the field not permitted.

Any idea ?

  • 1
    I know it has been a while, but I was looking for a solution to the same sort of problem and found a relevant answer: https://stackoverflow.com/a/51240745/9712989 – djangomachine Aug 17 '20 at 18:34

2 Answers2

2

I found a semi-solution :

from django.db.models.functions import Upper

MyModel.objects.all().annotate(
      upper=Upper('size_range')
  ).annotate(
      new_upper=F('upper') + 1
  )

Which feels clumsy but at least it werks

If anyone knows better ? I am sure it is possible to query directly the raw value.

0

What are you trying to do with it? You might be able to use endswith

MyModel.objects.filter(size_range__endswith=some_value))
schillingt
  • 13,493
  • 2
  • 32
  • 34