Skip to content

Issue with choices that are translations #17

Closed
@jole78

Description

@jole78

I have a model that looks something like this:

class Organisation(models.Model):

    CATEGORY_CHOICES = (
        (1, _('Government administrative authorities')),
        (2, _('Authorities under parliament')),
        (3, _('State utilities')),
        (4, _('AP fonds')),
        (5, _('Courts of Sweden and courts administration')),
        (6, _('Swedish foreign authorities')),
    )
    category = models.IntegerField(_('category'), 
        choices=CATEGORY_CHOICES
    )

The import part is that category is a choice that has a translation (which is in Swedish btw).
When I query for this field I get this (as an example):
"category": "STATLIGA_F_RVALTNINGSMYNDIGHETER"

...which looks pretty weird to me.

The schema looks like this:

class OrganisationNode(DjangoObjectType):

    class Meta:
        model = Organisation
        interfaces = (graphene.Node,)

If I instead do this:

 class OrganisationNode(DjangoObjectType):

    category = graphene.String()

    class Meta:
        model = Organisation
        interfaces = (graphene.Node,)

    def resolve_category(self, args, context, info):
        return self.get_category_display()

...the result is as expected, e.g.:
"category": "Statliga förvaltningsmyndigheter"

So, there is a way around it. I was just wondering if this is by design or...??

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions