Comments (2)
I found there is an undocumented behaviour in EnumField
, respectively EnumFieldMixin
. The choices are only built, if they're not an argument to the field class. So I could to:
enum_field_with_limited_choices = EnumField(
EnumWithManyChoices,
max_length=100,
choices=[
(EnumWithManyChoices.Choice1, EnumWithManyChoices.Choice1.label),
(EnumWithManyChoices.Choice2, EnumWithManyChoices.Choice2.label),
],
)
However, the serializer field drf.fields.EnumField
is building the choices again in it's __init__
-method. By changing this to
class EnumField(ChoiceField):
def __init__(self, enum, lenient=False, ints_as_names=False, **kwargs):
"""
:param enum: The enumeration class.
:param lenient: Whether to allow lenient parsing (case-insensitive, by value or name)
:type lenient: bool
:param ints_as_names: Whether to serialize integer-valued enums by their name, not the integer value
:type ints_as_names: bool
"""
self.enum = enum
self.lenient = lenient
self.ints_as_names = ints_as_names
# the if-block is new
if 'choices' not in kwargs:
kwargs['choices'] = tuple((e.value, getattr(e, 'label', e.name)) for e in self.enum)
super().__init__(**kwargs)
and the serializer mixin to copy over the choices from the model field
class EnumSupportSerializerMixin:
enumfield_options = {}
enumfield_classes_to_replace = (ChoiceField, CharField, IntegerField)
def build_standard_field(self, field_name, model_field):
field_class, field_kwargs = (
super().build_standard_field(field_name, model_field)
)
if isinstance(model_field, EnumFieldMixin) and field_class in self.enumfield_classes_to_replace:
field_class = EnumSerializerField
field_kwargs['enum'] = model_field.enum
field_kwargs['choices'] = [(enum.value, label) for enum, label in model_field.choices]
field_kwargs.update(self.enumfield_options)
return field_class, field_kwargs
the serializer would only offer and accept the choices defined on the model. Currently it's accepting all values of the enum, even though they might be excluded from the model. IMO, this is a bug and should be fixed
from django-enumfields.
I agree with @larsrinn
It should be really interesting to have the possibility to limite choices of enum class on serializer. I have this need currently
from django-enumfields.
Related Issues (20)
- Drop Python2 compatibility HOT 4
- Add blurb about included enumeration types in Django 3.0? 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
- 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.