Replies: 2 comments
-
|
Just a stab in the dark without trying to reproduce: Try using a StrEnum if you are on python 3.11+. Or otherwise add a __str__ function that returns Please try and let me know. I suspect you are encountering this because the default json serializer that dumpdata uses serializes Enum objects by calling str() or repr(). So the above suggestion should work for you. Alternatively I think the most appropriate place to fix this would be to provide a custom serializer that understands your objects. The suggestion above is certainly cleaner, but you might have to implement a serializer if you don't control the enum implementation. I don't think there's a way to modify django-enum to make serialization work for arbitrary enum types. #126 might fix this, but glancing through the code it doesn't appear to me that it obviously would. Serialization should work out of the box when using IntEnum/StrEnum which should satisfy most use cases. I would consider this mostly an upstream python issue. The default json serializer should serialize Enum types by calling enum.value. The fact that it doesn't has created problems for me before. Here's a stab at a Django json serializer that understands enums: # myapp/serializers/json.py
import json
import enum
from django.core.serializers.json import DjangoJSONEncoder
from django.core.serializers.json import Serializer as JSONSerializer
class EnumJSONEncoder(DjangoJSONEncoder):
"""
Extends Django's JSON encoder to support Python Enum objects by
returning their .value rather than their name or repr().
"""
def default(self, obj):
if isinstance(obj, enum.Enum):
return super().default(obj.value)
return super().default(obj)
class Serializer(JSONSerializer):
"""
JSON serializer that uses EnumJSONEncoder to handle Enum values.
"""
def end_serialization(self):
json.dump(
self.objects,
self.stream,
cls=EnumJSONEncoder,
**self.json_kwargs,
)Alternatively, there's a handle_field method on Django's JSONSerializer that could also be overridden. That might be cleaner. You would have to reference it directly when calling dumpdata: |
Beta Was this translation helpful? Give feedback.
-
|
Actually it would be possible to fix this in django-enum by overriding value_from_object but I would want to fully understand the side effects before I did that. So in the meantime, see above. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
We're using EnumField with pure Enums, whose values are all strings.
This worked fine for the half year since we adopted it, but now we had a reason to try and make database snapshots.
Django's dumpdata command dumps the value of such a field as e.g. "PlyState.CANCELLED", which guarantees an error on using loaddata.
I would assume the dumped value is wrong. For now we're manually (bash script) adjusting these dumped values (removing everything before the .) and then loaddata works.
If you need more information let me know.
Beta Was this translation helpful? Give feedback.
All reactions