Skip to content

to_const doesn't support unicode #1079

Closed

Description

@henrythor

Let's say I have a model in Django with a field that has choices. Only, because I'm Icelandic, those choices might be in Icelandic and contain utf-8 characters. Generating an enum from my model will fail.

F.ex.:

class School(models.Model):
    MUNICIPALITIES = (
        ('Hafnarfjarðarkaupstaður', 'Hafnarfjarðarkaupstaður'),
        ('Reykjanesbær', 'Reykjanesbær'),
        ('Garðabær', 'Garðabær'),
        ...
    )
    name = models.CharField(max_length=128)
    municipality = models.CharField(max_length=32, blank=True, null=True, choices=MUNICIPALITIES)

I then try to create a Query object:

class SchoolNode(DjangoObjectType):
    @classmethod
    class Meta:
        model = School
        filter_fields = [
            'id',
            'name',
        ]
        exclude_fields = [
        ]
        interfaces = (relay.Node,)


class Query(object):
    school = relay.Node.Field(SchoolNode)
    all_schools = DjangoFilterConnectionField(SchoolNode)

This will then fail in graphql.utils.assert_valid_name.assert_valid_name at schema evaluation since to_const doesn't handle converting unicode characters to ascii:
Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "A_HAFNARFJARÐARKAUPSTAÐUR" does not.

I really doubt I'm the only one running into this. Most people just handle this stuff differently I suppose? Manually switch out code, or use an actual Enum() in the choices?

My proposed solution is to use the unidecode package, specifically designed for this. I'll throw together a PR.

Activity

  1. stale commented on Jan 2, 2020

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

  2. ekampf commented on Feb 8, 2020

    @ekampf
    Contributor

    FYI fixed on #1080

  3. reopened this on Feb 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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