Skip to content

Allow CharField to define a generic enum type #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
bellini666 opened this issue Jul 14, 2021 · 3 comments · Fixed by #65
Closed

Allow CharField to define a generic enum type #34

bellini666 opened this issue Jul 14, 2021 · 3 comments · Fixed by #65
Labels
bug Something isn't working

Comments

@bellini666
Copy link

It would be nice to have support for this: https://docs.djangoproject.com/en/3.2/ref/models/fields/#enumeration-types

Maybe something like this:

class SomeEnum(models.TextFields):
    FOO = "foo"
    BAR = "bar"

some_enum_field = models.CharField[SomeEnum](  # note the [SomeEnum] generic here
    choices=SomeEnum.choices,
    default=SomeEnum.FOO,
)

This way the typing would know that the field reads and writes that exact enum type and not a str.

@sbdchd sbdchd added the bug Something isn't working label Jul 14, 2021
@sbdchd
Copy link
Owner

sbdchd commented Jul 14, 2021

Interesting, playing around with it a bit, and it seems like the Django API makes it a little tricky to do this, ideally we'd pass in SomeEnum as a param, and then we could use that for the @overload.

Something like

class FooModel(models.Model):

    class BarEnum(models.TextChoices):
        FOO = "foo"
        BAR = "bar"

    some_enum_field = models.CharField(
       # something like this would allow for an @overload
        enum=BarEnum,
        default=BarEnum.FOO,
    )

@sbdchd
Copy link
Owner

sbdchd commented Jul 14, 2021

Also by

reads and writes that exact enum type and not a str

Do you mean the set and get would be Literal["foo", "bar"]?

@bellini666
Copy link
Author

Hey @sbdchd ,

I always defined enums the old way without the models.TextChoices, and after seeing its existence yesterday I theocrafted that it would get and set the enum itself, but I just noticed that even though the set accepts the enum, the get reads it as a string, which is said :(

Having said that, allowing to declare the CharField that it get/set Literal["foo", "bar"] would be awesome! The same applies to IntegerField with choices defined.

@kodiakhq kodiakhq bot closed this as completed in #65 Dec 5, 2021
kodiakhq bot pushed a commit that referenced this issue Dec 5, 2021
This was built on top of #63, so merging it should only keep this PR's change

Note that even though the change looks big, it actually is pretty small, it is just that all fields need to define their own `__new__` (or `__init__` if their signature is different from the base field), and it is all the same, the difference is the classname and the generic field. I also added some documentation [here](https://github.com/sbdchd/django-types/pull/65/files#diff-876a5d116ef70c86039fbb8498623871324fa93fc1ae51c7c09ba2a0f7612e99R47) explaining the usage.

Fix #34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants