strawberry-django-plus
strawberry-django-plus copied to clipboard
FieldError when using select_related with enable_only_optimization
When trying to use select_related
on a field, I am getting an error when the DjangoOptimizerExtension
is used with its default enable_only_optimization=True
.
This happens because only()
is used with "id", and then select_related
is issued on top:
https://github.com/blb-ventures/strawberry-django-plus/blob/9a54dd0378b31302b99754704ccd9f1025f43fbd/strawberry_django_plus/optimizer.py#L529-L533
django.core.exceptions.FieldError: Field Foo.bar cannot be both deferred and traversed using select_related at the same time.
A workaround is to add the field also to only
, but that could be done automatically then also.
@django_type(models.Foo, filters=FooFilter, pagination=True)
class Foo:
bar: Bar
special_bar: Bar | None = gql.django.field(
only=["bar"], # workaround
select_related=["bar"]
)
I am not sure about the select_related
being necessary/useful here yet (maybe because of the Optional/None it might be necessary), but I think it's a generic issue.
I also wondered what's the point of enable_only_optimization
. Is it to reduce the amount of data being selected/transferred only?
(related)