question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Inconsistent behavior for ID field doing update in DjangoModelFormMutation

See original GitHub issue

ID field in DjangoModelFormMutation displaying Global ID, however doing updates form id requires raw DB pk field.

Say I have below scripts

# cookbook/ingredients/models.py
from django.db import models
class Category(models.Model):
    name = models.CharField(max_length=100)

    def __str__(self):
        return self.name

# cookbook/ingredients/forms.py
from django.forms.models import ALL_FIELDS, ModelForm
from cookbook.ingredients.models import Category

class CategoryForm(ModelForm):
    class Meta:
        model = Category
        fields = ALL_FIELDS

# cookbook/ingredients/schema.py
from graphene import Field, Node, ObjectType
from graphene_django.filter import DjangoFilterConnectionField
from graphene_django.forms.mutation import DjangoModelFormMutation
from graphene_django.types import DjangoObjectType

from cookbook.ingredients.forms import CategoryForm
from cookbook.ingredients.models import Category

class CategoryNode(DjangoObjectType):
    class Meta:
        model = Category
        interfaces = (Node,)
        filter_fields = ["name", "ingredients"]

class Queries(ObjectType):
    category = Node.Field(CategoryNode)
    all_categories = DjangoFilterConnectionField(CategoryNode)

class CategoryMutation(DjangoModelFormMutation):
    category = Field(CategoryNode)

    class Meta:
        form_class = CategoryForm

class Mutations(ObjectType):
    category_form_create = CategoryMutation.Field()
    category_form_update = CategoryMutation.Field()

while I do query allCategories like this

query {
  allCategories {
    edges {
      node {
        id
        name
      }
    }
  }
}

result as below

{
  "data": {
    "allCategories": {
      "edges": [
        {
          "node": {
            "id": "Q2F0ZWdvcnlOb2RlOjE=", // global id
            "name": "Dairy"
          }
        },
        {
          "node": {
            "id": "Q2F0ZWdvcnlOb2RlOjI=", // global id
            "name": "Meat"
          }
        }
      ]
    }
  }
}

above are expected

Now I would like to do some update

# query part
mutation xxx($formInput: CategoryMutationInput!) {
  categoryFormUpdate (input: $formInput){
    category {
      id
      name
    }
  }
}
// variables part
{
  "formInput": {
    "id": "Q2F0ZWdvcnlOb2RlOjE=", // <- using global id
    "name": "Diary 1"
  }
}

I got errors sth like Field 'id' expected a number but got 'Q2F0ZWdvcnlOb2RlOjE='.
so I decode this Q2F0ZWdvcnlOb2RlOjE= and get this CategoryNode:1 string

and then I change the id to raw DB pk in variables part

// variables part
{
  "formInput": {
    "id": 1, // <- using raw DB pk
    "name": "Diary 1"
  }
}

I got the expected result

{
  "data": {
    "categoryFormUpdate": {
      "category": {
        "id": "Q2F0ZWdvcnlOb2RlOjU=", // <- global id
        "name": "Diary 1"
      }
    }
  }
}

Is this behavior expected?
I think this is confusing and inconsistent (should be always global id, I think). Shouldn’t it be mentioned in the doc? Or Is it a bug?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:15
  • Comments:9

github_iconTop GitHub Comments

1reaction
zbyte64commented, Feb 19, 2020

Also related to PR #546 which was closed due to inactivity

0reactions
pfcodescommented, Oct 28, 2022

Anybody come up with a solution to this? Thinking I might just manually convert from the Global ID to the pk in perform_mutate

Read more comments on GitHub >

github_iconTop Results From Across the Web

graphql - Django + Graphene + DjangoModelFormMutation
Have the update mutation work partially as well , i.e only updating the client-provided fields. How can I achieve the desired behavior ?...
Read more >
Mutations - Graphene-Python
ID () # The class attributes define the response of the mutation question = graphene.Field(QuestionType) @classmethod def mutate(cls, root, info, text, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found