Comments (4)
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.
from django-enumfields.
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.
from django-enumfields.
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 Enum
s 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 Enum
s, so I preferred to stay with this package. But now that I know they are true Enum
s (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.
from django-enumfields.
I created a Django issue related to improving the docs in this area: https://code.djangoproject.com/ticket/33193
from django-enumfields.
Related Issues (20)
- Drop Python2 compatibility HOT 4
- Automatically register EnumFieldListFilter
- faced this while trying to upgrade a django 1.8 project to django 2.2/3.0 HOT 8
- New release on PyPI HOT 2
- Customize enum choices in serializer HOT 2
- 2.1.0 Incompatibility with drf-yasg HOT 2
- DRF : Object of type Gender is not JSON serializable when calling OPTIONS request HOT 1
- Limit choices of an enum HOT 2
- Migrate to new CI platform HOT 5
- Python 3.11 Support HOT 1
- Allow to serialize enum by names HOT 2
- Breaks with django 2.0 HOT 4
- Calculate max_length automatically? HOT 2
- Deprecation warning in Django 2.1.1 HOT 2
- Support Field attributes (like 'name', etc) HOT 1
- Accept subset of the Enum values HOT 2
- Incompatibility with django-filter HOT 1
- Is the project alive? HOT 13
- Includes tests in source tarball
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-enumfields.