Skip to content

Annotations in strawberry_django.field Remove get_queryset Annotations #705

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
pfcodes opened this issue Feb 13, 2025 · 3 comments
Open
Labels
bug Something isn't working

Comments

@pfcodes
Copy link

pfcodes commented Feb 13, 2025

Describe the Bug

When using strawberry_django.field(annotate=...), any additional annotations added in get_queryset are removed. This makes it difficult to dynamically modify the queryset while keeping the annotation intact.

Reproduction Steps

  1. Define a Strawberry Django type with an annotated field:
import strawberry
import strawberry_django
from django.db.models import Count, Q

from myapp.models import JobApplicationModel, JobModel

@strawberry_django.type(JobModel)
class JobType:
    application_count: int = strawberry_django.field(
        annotate=Count(
            "job_applications",
            filter=Q(job_applications__status=JobApplicationModel.Status.SUBMITTED),
            distinct=True,
        )
    )

    @classmethod
    def get_queryset(cls, queryset, info):
        return queryset.annotate(
            another_annotation=Count("some_other_related_field")
        )
  1. The expected behavior is that application_count should persist, and another_annotation should be added in get_queryset.
  2. However, when get_queryset runs, the another_annotation is removed.

System Information

  • Strawberry version (if applicable): 0.237.3
  • Strawberry Django version: 0.47.1

Additional Context

@pfcodes pfcodes added the bug Something isn't working label Feb 13, 2025
@pfcodes
Copy link
Author

pfcodes commented Feb 13, 2025

I resolved this by simply using strawberry_django.field(annotate=...) for the property I was annotating and got rid of it from get_queryset()

@pfcodes pfcodes closed this as completed Feb 13, 2025
@pfcodes
Copy link
Author

pfcodes commented Feb 13, 2025

Re-opening because I actually do need it to live in get_queryset() in order to properly access the annotation even when the field isn't queried.

@pfcodes pfcodes reopened this Feb 13, 2025
@bellini666
Copy link
Member

bellini666 commented Feb 27, 2025

Hi @pfcodes ,

That annotate parameter on strawberry_django.field gets added by the optimizer, and it will run after get_queryset

I'm wondering here if what you are seeing is get_queryset not running at all or something else

However, when get_queryset runs, the another_annotation is removed.

Just to be clean, you mean that when get_queryset runs, the application_count gets removed, right? Because get_queryset is what annotates another_annotation


Also, how have you seen that the annotation is not there? Have you gotten any errors or were you checking it during get_queryset or some other place?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants