Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions graphene_django/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand All @@ -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
Expand Down Expand Up @@ -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)
12 changes: 12 additions & 0 deletions graphene_django/filter/filterset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
29 changes: 29 additions & 0 deletions graphene_django/filter/tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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")
65 changes: 65 additions & 0 deletions graphene_django/tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

32 changes: 32 additions & 0 deletions graphene_django/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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