Skip to content
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

Add blurb about included enumeration types in Django 3.0? #111

Open
adamchainz opened this issue Jan 26, 2020 · 4 comments
Open

Add blurb about included enumeration types in Django 3.0? #111

adamchainz opened this issue Jan 26, 2020 · 4 comments

Comments

@adamchainz
Copy link

Hi,

Great package, I just learnt about it from a friend that's using it. Django 3.0 includes enumeration types which seem to duplicate much of the functionality here.

Perhaps a link in the documentation would be nice, to guide users finding this library?

Thanks,

Adam

@jalaziz
Copy link

jalaziz commented Apr 4, 2021

Looking at the code, it seems like this package would actually work just fine with Django's enumeration types. About to test it out, but it might be good to provide examples that use Django's Choices class and its subclasses.

@johnthagen
Copy link
Contributor

Something I didn't realize the first time I read about the new Django 3.0+ enumeration types is that they are in fact "real" Python enums.

If you look at the Django source code:

class ChoicesMeta(enum.EnumMeta):
    """A metaclass for creating a enum choices."""

...

class Choices(enum.Enum, metaclass=ChoicesMeta):
    """Class for creating enumerated choices."""

They are really just Enums with some extra methods added onto them. My hesitation with using them when I first read about them is that it wasn't clear that they are truly Python Enums, so I preferred to stay with this package. But now that I know they are true Enums (not something that is totally Django-specific other than a few helper methods) I'm planning to move to them.

I hope this helps others who might have had a similar concern.

@johnthagen
Copy link
Contributor

I created a Django issue related to improving the docs in this area: https://code.djangoproject.com/ticket/33193

@johnthagen
Copy link
Contributor

johnthagen commented Oct 14, 2021

As noted at the bottom of https://stackoverflow.com/a/58051918, one (in my view big) differences between Django's native enumeration types and django-enumfield is that Django's returns a str when accessed rather than the actual Enum instance.

That means you can do the following in django-enumfields but not with Django Choices:

if my_model.color is Color.Red:
    ...

which is the preferred way to do type-safe Enum evaluation in Python.

With Django Choices you'd need to do something like:

if Color[my_model.color] is Color.Red:
    ...

or create a helper method for each Enum field to do this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants