diff --git a/graphene_django/converter.py b/graphene_django/converter.py index 467804dab..ae387d894 100644 --- a/graphene_django/converter.py +++ b/graphene_django/converter.py @@ -144,7 +144,9 @@ def generate_enum_name(django_model_meta, field): def convert_choice_field_to_enum(field, name=None): if name is None: name = generate_enum_name(field.model._meta, field) - choices = field.choices + choices = getattr(field, "choices", None) or getattr( + getattr(field, "output_field", None), "choices", None + ) return convert_choices_to_named_enum_with_descriptions(name, choices) @@ -155,7 +157,9 @@ def convert_django_field_with_choices( converted = registry.get_converted_field(field) if converted: return converted - choices = getattr(field, "choices", None) + choices = getattr(field, "choices", None) or getattr( + getattr(field, "output_field", None), "choices", None + ) if convert_choices_to_enum is None: convert_choices_to_enum = bool( graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CONVERT @@ -511,6 +515,21 @@ def convert_postgres_range_to_string(field, registry=None): ) +if hasattr(models, "GeneratedField"): + + @convert_django_field.register(models.GeneratedField) + def convert_field_to_generated_field(field, registry=None): + output_field = field.output_field.clone() + output_field.model = getattr(field, "model", None) + output_field.name = getattr(field, "name", None) + output_field.null = field.null + if field.help_text: + output_field.help_text = field.help_text + if getattr(field, "choices", None): + output_field.choices = field.choices + return convert_django_field(output_field, registry) + + # Register Django lazy()-wrapped values as GraphQL description/help_text. # This is needed for using lazy translations, see https://github.com/graphql-python/graphql-core-next/issues/58. register_description(Promise) diff --git a/graphene_django/filter/filterset.py b/graphene_django/filter/filterset.py index 7e0d0c520..508a44893 100644 --- a/graphene_django/filter/filterset.py +++ b/graphene_django/filter/filterset.py @@ -29,6 +29,18 @@ class GrapheneFilterSetMixin(BaseFilterSet): ) ) + @classmethod + def filter_for_field(cls, field, field_name, lookup_expr=None): + if hasattr(models, "GeneratedField") and isinstance( + field, models.GeneratedField + ): + output_field = field.output_field.clone() + output_field.model = getattr(field, "model", None) + output_field.name = getattr(field, "name", None) + output_field.null = field.null + field = output_field + return super().filter_for_field(field, field_name, lookup_expr=lookup_expr) + def setup_filterset(filterset_class): """Wrap a provided filterset in Graphene-specific functionality""" diff --git a/graphene_django/filter/tests/test_fields.py b/graphene_django/filter/tests/test_fields.py index b9c8df465..58b4eaa5d 100644 --- a/graphene_django/filter/tests/test_fields.py +++ b/graphene_django/filter/tests/test_fields.py @@ -2,6 +2,7 @@ from textwrap import dedent import pytest +from django.db import models from django.db.models import TextField, Value from django.db.models.functions import Concat @@ -1302,3 +1303,31 @@ class Meta: field = DjangoFilterConnectionField(ReporterFilterNode) assert_arguments(field, "some_filter") + + +@pytest.mark.skipif( + not hasattr(models, "GeneratedField"), + reason="GeneratedField is only supported in Django 5.0+", +) +def test_generated_field_filter(): + class Rectangle(models.Model): + class Meta: + app_label = "filter_tests" + + width = models.IntegerField() + height = models.IntegerField() + area = models.GeneratedField( + expression=models.F("width") * models.F("height"), + output_field=models.IntegerField(), + db_persist=True, + ) + + class RectangleNode(DjangoObjectType): + class Meta: + model = Rectangle + interfaces = (Node,) + fields = ("width", "height", "area") + filter_fields = {"area": ["exact", "gt", "lt"]} + + field = DjangoFilterConnectionField(RectangleNode) + assert_arguments(field, "area", "area__gt", "area__lt") diff --git a/graphene_django/tests/test_converter.py b/graphene_django/tests/test_converter.py index d08d07b11..2ac1677b1 100644 --- a/graphene_django/tests/test_converter.py +++ b/graphene_django/tests/test_converter.py @@ -540,3 +540,68 @@ class Mutation(graphene.ObjectType): ) assert not result.errors assert result.data["createReporter"]["reporter"] == expected_reporter + + +@pytest.mark.skipif( + not hasattr(models, "GeneratedField"), + reason="GeneratedField is only supported in Django 5.0+", +) +def test_should_generated_field_convert(): + field = models.GeneratedField( + expression=models.F("a") * 2, + output_field=models.IntegerField(), + db_persist=True, + null=True, + help_text="Calculated int", + ) + graphene_type = convert_django_field(field) + assert isinstance(graphene_type, graphene.Int) + mounted_field = graphene_type.Field() + assert mounted_field.description == "Calculated int" + assert mounted_field.type == graphene.Int + + nonnull_field = models.GeneratedField( + expression=models.F("a") * 2, + output_field=models.DecimalField(max_digits=10, decimal_places=2), + db_persist=True, + null=False, + ) + graphene_type = convert_django_field(nonnull_field) + assert isinstance(graphene_type, graphene.Decimal) + mounted_field = graphene_type.Field() + assert isinstance(mounted_field.type, graphene.NonNull) + assert mounted_field.type.of_type == graphene.Decimal + + +@pytest.mark.skipif( + not hasattr(models, "GeneratedField"), + reason="GeneratedField is only supported in Django 5.0+", +) +def test_generated_field_with_choices_convert_enum(): + field = models.GeneratedField( + expression=models.F("code"), + output_field=models.CharField( + max_length=2, + choices=(("es", "Spanish"), ("en", "English")), + ), + db_persist=True, + ) + + class ChoicesGeneratedModel(models.Model): + language = field + + class Meta: + app_label = "test" + + converted = convert_django_field_with_choices(field) + graphene_type = ( + converted.type.of_type + if isinstance(converted.type, graphene.NonNull) + else converted.type + ) + assert graphene_type._meta.name == "TestChoicesGeneratedModelLanguageChoices" + assert graphene_type._meta.enum.__members__["ES"].value == "es" + assert graphene_type._meta.enum.__members__["ES"].description == "Spanish" + assert graphene_type._meta.enum.__members__["EN"].value == "en" + assert graphene_type._meta.enum.__members__["EN"].description == "English" + diff --git a/graphene_django/tests/test_types.py b/graphene_django/tests/test_types.py index 0491bcd30..b262998c8 100644 --- a/graphene_django/tests/test_types.py +++ b/graphene_django/tests/test_types.py @@ -832,3 +832,35 @@ class Query(ObjectType): assert "type Reporter implements Node {" not in schema assert "type ReporterConnection {" not in schema assert "type ReporterEdge {" not in schema + + +@pytest.mark.skipif( + not hasattr(models, "GeneratedField"), + reason="GeneratedField is only supported in Django 5.0+", +) +@with_local_registry +def test_django_objecttype_generated_field(): + class Rectangle(models.Model): + class Meta: + app_label = "tests" + + width = models.IntegerField() + height = models.IntegerField() + area = models.GeneratedField( + expression=models.F("width") * models.F("height"), + output_field=models.IntegerField(), + db_persist=True, + help_text="Calculated area", + ) + + class RectangleType(DjangoObjectType): + class Meta: + model = Rectangle + fields = ("width", "height", "area") + + class Query(ObjectType): + rect = Field(RectangleType) + + schema = Schema(query=Query) + schema_str = str(schema) + assert "area: Int" in schema_str