Coverage for extras/graphql/filter_lookups.py: 57%

17 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-10 18:35 +0000

1import strawberry 

2import strawberry_django 

3from django.db.models import Q, QuerySet 

4from strawberry.directive import DirectiveValue 

5from strawberry.types import Info 

6 

7__all__ = ( 

8 'ExtraChoicesLookup', 

9) 

10 

11 

12@strawberry.input( 

13 one_of=True, 

14 description='Lookup for extra choices defined on a choice set. Only one of the lookup fields can be set.', 

15) 

16class ExtraChoicesLookup: 

17 contains: str | None = strawberry.field( 

18 default=strawberry.UNSET, description='Has an extra choice with this value' 

19 ) 

20 length: int | None = strawberry.field( 

21 default=strawberry.UNSET, description='Number of extra choices' 

22 ) 

23 

24 @strawberry_django.filter_field 

25 def filter(self, info: Info, queryset: QuerySet, prefix: DirectiveValue[str] = '') -> tuple[QuerySet, Q]: 

26 if self.contains is not strawberry.UNSET and self.contains is not None: 

27 return queryset, Q(**{f'{prefix}choice_value': self.contains}) 

28 if self.length is not strawberry.UNSET and self.length is not None: 

29 return queryset, Q(**{f'{prefix}len': self.length}) 

30 return queryset, Q()