graphql-python / graphql-python/graphene-django

Inconsistent behavior for ID field doing update in DjangoModelFormMutation

Open
#867 9 comments 20 reactions 0 assignees View on GitHub
🐛bug
Dominant language
Python
Stars
4.4k
Forks
760
PR merge metrics
No merged PRs in 30d

Description

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

Say I have below scripts
```python
# 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
```shell
query {
allCategories {
edges {
node {
id
name
}
}
}
}
```

result as below
```javascript
{
"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
```shell
# query part
mutation xxx($formInput: CategoryMutationInput!) {
categoryFormUpdate (input: $formInput){
category {
id
name
}
}
}
```
```javascript
// 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`
```javascript
// variables part
{
"formInput": {
"id": 1, // <- using raw DB pk
"name": "Diary 1"
}
}
```
I got the expected result
```javascript
{
"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?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.