Skip to content

Key error #18

@taniagmangolini

Description

@taniagmangolini

I am facing the following error when I try to create an object. Could you help me?
payload = {
'name': 'Forest Test T',
'domain': DomainsType.mata_atlantica,
}
res = self.client.post(FOREST_URL, payload)

Error:
Traceback (most recent call last):
File "/app/forest/tests/test_forest_api.py", line 66, in test_create_forest
res = self.client.post(FOREST_URL, payload)
File "/py/lib/python3.9/site-packages/rest_framework/test.py", line 296, in post
response = super().post(
File "/py/lib/python3.9/site-packages/rest_framework/test.py", line 210, in post
return self.generic('POST', path, data, content_type, **extra)
File "/py/lib/python3.9/site-packages/rest_framework/test.py", line 234, in generic
return super().generic(
File "/py/lib/python3.9/site-packages/django/test/client.py", line 541, in generic
return self.request(**r)
File "/py/lib/python3.9/site-packages/rest_framework/test.py", line 286, in request
return super().request(**kwargs)
File "/py/lib/python3.9/site-packages/rest_framework/test.py", line 238, in request
request = super().request(**kwargs)
File "/py/lib/python3.9/site-packages/django/test/client.py", line 810, in request
self.check_exception(response)
File "/py/lib/python3.9/site-packages/django/test/client.py", line 663, in check_exception
raise exc_value
File "/py/lib/python3.9/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/py/lib/python3.9/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/py/lib/python3.9/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/py/lib/python3.9/site-packages/rest_framework/viewsets.py", line 125, in view
return self.dispatch(request, *args, **kwargs)
File "/py/lib/python3.9/site-packages/rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "/py/lib/python3.9/site-packages/rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "/py/lib/python3.9/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
File "/py/lib/python3.9/site-packages/rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "/py/lib/python3.9/site-packages/rest_framework/mixins.py", line 18, in create
serializer.is_valid(raise_exception=True)
File "/py/lib/python3.9/site-packages/rest_framework/serializers.py", line 227, in is_valid
self._validated_data = self.run_validation(self.initial_data)
File "/py/lib/python3.9/site-packages/rest_framework/serializers.py", line 426, in run_validation
value = self.to_internal_value(data)
File "/py/lib/python3.9/site-packages/rest_framework/serializers.py", line 483, in to_internal_value
validated_value = field.run_validation(primitive_value)
File "/py/lib/python3.9/site-packages/rest_framework/fields.py", line 568, in run_validation
value = self.to_internal_value(data)
File "/py/lib/python3.9/site-packages/rest_framework/fields.py", line 1915, in to_internal_value
return self.model_field.to_python(data)
File "/py/lib/python3.9/site-packages/enumchoicefield/fields.py", line 63, in to_python
return self.enum[value]
File "/usr/local/lib/python3.9/enum.py", line 432, in getitem
return cls.member_map[name]

This is my class:

from enumchoicefield import ChoiceEnum

class DomainsType(ChoiceEnum):
amazonia = 'Amazonia'
mata_atlantica = 'Mata Atlantica'
caatinga = 'Caatinga'
cerrado = 'Cerrado'
pantanal = 'Pantanal'
pradaria = 'Pradaria'

My model:
class Forest(BaseModel):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=500, unique=True)
domain = EnumChoiceField(enum_class=DomainsType)

Versions:

  • Django>=3.2.16
  • djangorestframework==3.13.1
  • django-enumchoicefield==3.0.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions