Inconsistent behavior for ID field doing update in DjangoModelFormMutation
See original GitHub issueID
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:
- Created 4 years ago
- Reactions:15
- Comments:9
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Also related to PR #546 which was closed due to inactivity
Anybody come up with a solution to this? Thinking I might just manually convert from the Global ID to the
pk
inperform_mutate