sbdchd / django-types

:doughnut: Type stubs for Django
MIT License
186 stars 62 forks source link

Django ORM query annotation with `Right` subparam does not accept `F()` expression #233

Open yvsssantosh-condor opened 5 months ago

yvsssantosh-condor commented 5 months ago

I have a django query where I'm splitting a string and getting a part of it in an ORM query. I get an error which says,

Argument of type "F" cannot be assigned to parameter "length" of type "Value | int" in the function "__init__"
Type "F" cannot be assigned to type "Value | int"
"F" is incompatible with "Value"
"F" is incompatible with "int"

Here is my query:


# MyModel fields
# abc_id

# Example abc_ids: `123_1`, `123_2`, `123_3`. `124_1`, `125_1`, etc.

from django.db.models import F, IntegerField, Value
from django.db.models.functions import Cast, Reverse, Right, StrIndex

MyModel.objects.annotate(
    last_index_of_underscore=StrIndex(Reverse(F('abc_id')), Value('_')),
    abc_no=Cast(
        Right('abc_id', F('last_index_of_underscore') - 1), IntegerField()
    )
)