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

Add DjangoFormInputObjectType to forms/types #1325

Merged

Conversation

ndpu
Copy link
Contributor

@ndpu ndpu commented Jun 14, 2022

Adds DjangoFormInputObjectType (InputObjectType derived class) which can retrieve fields from django form. Type of fields with choices (which was converted to enum automatically) is set to custom scalar type (if object_type is specified in Meta) to dynamically convert enum values back.

With DjangoFormInputObjectType we can build input types with data from different models, nest it in one another etc. It's just like an ordinary input object type that have same fields as django form.

Enum values converting feature can be convenient, please comment your thoughts about this.

Example:

class MyDjangoFormInput(DjangoFormInputObjectType):

    # any other fields can be placed here as well as
    # other djangoforminputobjects and intputobjects

    class Meta:
        form_class = MyDjangoModelForm
        object_type = MyModelType


class SomeMutation(graphene.Mutation):
    class Arguments:
        data = MyDjangoFormInput(required=True)

    @staticmethod
    def mutate(_root, _info, data):

        # any data field (which have choices in django)
        # with value 'A_1' (for example) from client
        # is already converted to '1' here!

        form_inst = MyDjangoModelForm(data=data)
        if form_inst.is_valid():
            django_model_instance = form_inst.save(commit=False)

        # ...

@firaskafri
Copy link
Collaborator

@ndpu Hello, sorry this took long
Are you willing to revisit this so we can merge it? Tests are failing would you check please?

@ndpu
Copy link
Contributor Author

ndpu commented Apr 12, 2023

@firaskafri hi, ok, will do

@firaskafri
Copy link
Collaborator

@firaskafri hi, ok, will do

Hello there! Any updates?

ndpu added 6 commits May 5, 2023 03:40
InputObjectType derived class which gets fields from django form.
Type of fields with choices (converted to enum) is set to custom scalar
type (using Meta.object_type) to dynamically convert enum values back.
@ndpu ndpu force-pushed the feature/django-forms-inputobjecttype branch from 6ceba53 to e90edc1 Compare May 5, 2023 02:51
@ndpu
Copy link
Contributor Author

ndpu commented May 5, 2023

@firaskafri fixed tests and pre-commit hook error. Should be ok now. (force-pushed rebased branch...)

@firaskafri
Copy link
Collaborator

@ndpu would you also add your example to the docs please?

@ndpu
Copy link
Contributor Author

ndpu commented May 11, 2023

@firaskafri hi, docs added. I have an error with sphinx 1.5.3 and all is ok with latest version (7.0.0). I think docs/requirements.txt should be updated...

@firaskafri firaskafri merged commit be17278 into graphql-python:main May 24, 2023
12 checks passed
superlevure pushed a commit to loft-orbital/graphene-django that referenced this pull request Jul 19, 2023
* Add DjangoFormInputObjectType to forms/types

InputObjectType derived class which gets fields from django form.
Type of fields with choices (converted to enum) is set to custom scalar
type (using Meta.object_type) to dynamically convert enum values back.

* Correct Reporter model a_choice field type according to CHOICES tuple

* Add tests for DjangoFormInputObjectType

* Add pyenv files to .gitignore

* Fix pyupgrade

* Fix tests

* Add docs

* Fix docs example

---------

Co-authored-by: Firas Kafri <3097061+firaskafri@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants