Opened 21 months ago

Last modified 5 weeks ago

#22288 new Bug

F() expression not compatible with __range field look up

Reported by: liushaohua86@… Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords:
Cc: github@… Triage Stage: Accepted
Has patch: yes Needs documentation:
Needs tests: Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description (last modified by timgraham)

class TestModel(models.Model):
    a = models.SmallIntegerField()
    b = models.SmallIntegerField()

TestModel.objects.filter(a__range=(F('b')-1, F('b')+1)

TypeError: int() argument must be a string or a number, not 'ExpressionNode'

Change History (7)

comment:2 Changed 21 months ago by bmispelon

  • Triage Stage changed from Unreviewed to Accepted
  • Version changed from 1.5 to master


I can reproduce this issue on latest master as well as on 1.6 so it doesn't appear to be related to the new custom lookup feature.

Regarding your formatting issues, you can make code blocks on trac using {{{ code goes here }}}, as explained on the WikiFormatting page.


comment:3 Changed 19 months ago by timmartin

  • Has patch set

I've had a go at fixing this, the commit is here:

I'd like feedback on this fix, since I'm not sure I fully understand the potential wider impact of the changes I've made (though it passes all regression tests). It also looks like the changes in the pull request for #14030 will interact with this.

comment:4 Changed 16 months ago by akaariai

  • Patch needs improvement set

I checked the patch. It seems we need changes to too many places just to support something__range=(F('foo'), F('bar')). The main issue is that we have many rounds of value preparation.

The main issue here is that we just don't handle the case here value is an iterable correctly for value preparation. While the above mentioned commit touches the area around range lookups, we should likely fix all the lookups to lists of F() objects properly. Even if the only other example in core is somefield__in=[F()].

It might be better to wait for #14030 before applying this one.

In addition the tests of the above patch aren't correct, the code checks generated SQL, but that SQL differs between backends (quote character is not always ", and some backends IIRC use upper case table names).

Sorry for delay in commenting this issue.

comment:5 Changed 3 months ago by Naddiseo

  • Cc github@… added

Any chance that this can be looked into now?

comment:6 Changed 3 months ago by jarshwah

I think a side effect of is that range and in lookups will now understand expressions properly. I'm not certain of this though, it just looks like similar code paths.

In fact, I'll incorporate the test case above into this patch to check.

Last edited 3 months ago by jarshwah (previous) (diff)

comment:7 Changed 3 months ago by jarshwah

Nope, the linked patch doesn't alter the path at all.

comment:8 Changed 5 weeks ago by timgraham

  • Description modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top