Skip to content
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

Support Django 5 GeneratedField #1494

Open
KianChavoshiNejad opened this issue Jan 26, 2024 · 2 comments
Open

Support Django 5 GeneratedField #1494

KianChavoshiNejad opened this issue Jan 26, 2024 · 2 comments

Comments

@KianChavoshiNejad
Copy link

Note: for support questions, please use stackoverflow. This repository's issues are reserved for feature requests and bug reports.

  • What is the current behavior?

I used Django 5's GenerativeField to define one of my fields

quantity = models.PositiveIntegerField()

unit_price = models.DecimalField(
        max_digits=12,
        decimal_places=2,
        null=True,
        blank=True,
    )

total_price = models.GeneratedField(
        expression=models.F("quantity") * models.F("unit_price"),
        output_field=models.DecimalField(
            max_digits=12,
            decimal_places=2,
        ),
        db_persist=True,
        null=True,
        blank=True,
    )

When I try to pass the value through the DjangoObjectType, I received the following error:

Exception: Don't know how to convert the Django field ModelName.total_price (<class 'django.db.models.fields.generated.GeneratedField'>)

  1. Create a model with a GeneratedField
  2. Create a DjangoObjectType for the model created in step 1
  3. Error will appear when trying to query the generated field in GraphQL
  • What is the expected behavior?
    The expected behavior is for DjangoObjectType to use the output_field type defined in the GeneratedField.

  • What is the motivation / use case for changing the behavior?
    We would like to leverage the new GeneratedField functionality in Django 5.

  • Please tell us about your environment:

    • Version: 14,2,1
    • Platform: macOS Sonoma
    • Django_version = 5.0.1
      -Graphene_version = 3.3
  • Other information (e.g. detailed explanation, stacktraces, related issues, suggestions how to fix, links for us to have context, eg. stackoverflow)

@kiendang kiendang changed the title Exception: Don't know how to convert the Django field ModelName.total_price (<class 'django.db.models.fields.generated.GeneratedField'>) Support Django 5 GeneratedField Jan 27, 2024
@cmhac
Copy link

cmhac commented Jul 11, 2024

Also running into a use case for this in a project I'm working on

@andrewcole
Copy link

I don't know what I'm doing, but the following code worked for me

from django.contrib.gis.db import models

from graphene_django.converter import convert_django_field


@convert_django_field.register(models.GeneratedField)
def convert_generated_field(field, registry=None):
    convert_django_field.dispatch(field.output_field.__class__)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants