graphene-django-extras icon indicating copy to clipboard operation
graphene-django-extras copied to clipboard

DjangoInputObjectType doesn't allow overwriting model fields

Open ollamh opened this issue 6 years ago • 2 comments

Problem

I'm using custom logic for input validation of a model field (like, validate it as a string and then convert it into UUID v.5 before save). So, I have a model and schema defined below:

# models.py
class TestModel(models.Model):
    test_field = models.UUIDField()

# schema.py
class TestModelInput(DjangoInputObjectType):
    test_field = graphene.String(required=True)

    class Meta:
        model = TestModel

class CreateTestModel(graphene.Mutation):

    class Input:
        data = TestModelInput()
    
    @staticmethod
    def mutate(root, info, data):
        # here I convert and save my model

What I expect from printing graphql schema:

input TestModelInput {
  test_field: String!
  ...

What I get:

input TestModelInput {
  test_field: UUID!
  ...

Solution

I found out that in the graphene_django_extras/types.py:182 this line

super(InputObjectType, cls).__init_subclass_with_meta__(_meta=_meta, **options)

means that the method InputObjectType.__init_subclass_with_meta__ itself is not being called and this class attributes are not being converted into fields. The line should be changed into

super(DjangoInputObjectType, cls).__init_subclass_with_meta__(_meta=_meta, **options)

to run correctly.

P.S. Thanks for the great package! It saved me lots of work =)

ollamh avatar Jun 22 '18 13:06 ollamh

Hi @ollamh, could you tell me in enviroment where you've tried this fix, I'm trying Windows and it works fine, but in Debian it gives me problems: photo_2018-07-21_02-27-29

and after revert your solution all work fine again, any idea why?

eamigo86 avatar Jul 21 '18 00:07 eamigo86

Hi @eamigo86 !

Sorry for a delayed response. It looks like options dictionary already contains _meta key. Could you provide the call parameters? I'm using Ubuntu 16.04, python 2.7.12 and python 3.5 and it works as expected.

ollamh avatar Jul 23 '18 07:07 ollamh