graphql-python / graphql-python/graphene-django
Translatable Model Fields do not get listed in DjangoObjectType
- Dominant language
- Python
- Stars
- 4.4k
- Forks
- 760
- PR merge metrics
- No merged PRs in 30d
Description
Currently, if I have a DjangoObjectType node whose model contains django-parler's TranslatedFields, these do not get listed there and cannot be queried, mutated, etc...
```
import graphene
from graphene_django.types import DjangoObjectType
from parler.models import TranslatableModel, TranslatedFields
class MyModel(TranslatableModel):
content_translations = TranslatedFields(
title = models.CharField(max_length=200, default=""),
)
class MyNode(DjangoObjectType):
class Meta:
model = MyModel
class MyQuery(graphene.ObjectType):
node = graphene.List(MyNode)
```
I would expect for example, the following query to be valid:
```
query{
node{
title
}
}
```
The root-cause seems to lie in the way the model fields are populated inside `utils.py`:
```
def get_model_fields(model):
local_fields = [
(field.name, field)
for field in sorted(
list(model._meta.fields) + list(model._meta.local_many_to_many)
)
]
```
The following makes the translatable fields available, but it is not enough for the query to succeed:
```
def get_model_fields(model):
local_fields = [
(field.name, field)
for field in list(model._meta.get_fields()) # sorted() doesn't work for OneToMany relationship. get_fields() has been available for a while in django
]
```
As a workaround I can define each translatable field as an extra field in my query object, though this is not very scalable.
I'm running on Ubuntu 64 bit with the following python packages.
```
django-parler===2.2
graphene===2.1.8
graphene-django===2.15.0
```
Thanks in advance!
Contributor guide
Assessment
This issue has not been assessed yet.