django-rest-polymorphic
django-rest-polymorphic copied to clipboard
Serializer validation error
Even though i am passing valid data, I am getting below error. My serializer class
class QuestionCompoundSerializer(PolymorphicSerializer):
model_serializer_mapping = {
TOEFL_RCMCQ: TOEFL_RCMCQ_Serializer,
TOEFL_LCMCQ: TOEFL_LCMCQ_Serializer,
TOEFL_LCMCQ_WITH_PLAYBACK: TOEFL_LCMCQ_WITH_PLAYBACK_Serializer
}
question = QuestionCompoundSerializer(*data)
question.is_valid() giving below error.
Cannot resolve keyword ' ' into field. Choices are: abusable_tags, attribution_text, author, author_id, author_uuid, concept_uuid, created_at, deleted, difficulty_level, document_id, drive_url, estimated_reading_duration, file, file_type, modified_at, polymorphic_ctype, polymorphic_ctype_id, published, question_set_elements, questions, resource_type, source, subject_names, subject_uuids, target_assessment_types, target_exams, text_html, title_external, title_internal, topic_names, topic_uuids, uuid
What is data
here? Is it a dictionary? Because I think that you should instantiate your serializer like this:
question = QuestionCompoundSerializer(data=data)
question = QuestionCompoundSerializer(data=request_data) question.is_valid(raise_exception=True) question.save()
Even this throws same error
Traceback (most recent call last): File "/home/maruthi/.virtualenvs/dmango/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner response = get_response(request) File "/home/maruthi/.virtualenvs/dmango/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response response = self.process_exception_by_middleware(e, request) File "/home/maruthi/.virtualenvs/dmango/lib/python3.6/site-packages/django/core/handlers/base.py", line 124, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/home/maruthi/.virtualenvs/dmango/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view return view_func(*args, **kwargs) File "/home/maruthi/.virtualenvs/dmango/lib/python3.6/site-packages/rest_framework/viewsets.py", line 116, in view return self.dispatch(request, *args, **kwargs) File "/home/maruthi/.virtualenvs/dmango/lib/python3.6/site-packages/rest_framework/views.py", line 495, in dispatch response = self.handle_exception(exc) File "/home/maruthi/.virtualenvs/dmango/lib/python3.6/site-packages/rest_framework/views.py", line 455, in handle_exception self.raise_uncaught_exception(exc) File "/home/maruthi/.virtualenvs/dmango/lib/python3.6/site-packages/rest_framework/views.py", line 492, in dispatch response = handler(request, *args, **kwargs) File "/home/maruthi/websites/dmango-entrayn/dmango/questions/views.py", line 75, in create serializer_object.is_valid(raise_exception=True) File "/home/maruthi/.virtualenvs/dmango/lib/python3.6/site-packages/rest_polymorphic/serializers.py", line 82, in is_valid valid = super(PolymorphicSerializer, self).is_valid(*args, **kwargs) File "/home/maruthi/.virtualenvs/dmango/lib/python3.6/site-packages/rest_framework/serializers.py", line 236, in is_valid self._validated_data = self.run_validation(self.initial_data) File "/home/maruthi/.virtualenvs/dmango/lib/python3.6/site-packages/rest_framework/serializers.py", line 434, in run_validation value = self.to_internal_value(data) File "/home/maruthi/.virtualenvs/dmango/lib/python3.6/site-packages/rest_polymorphic/serializers.py", line 67, in to_internal_value ret = serializer.to_internal_value(data) File "/home/maruthi/.virtualenvs/dmango/lib/python3.6/site-packages/rest_framework/serializers.py", line 491, in to_internal_value validated_value = field.run_validation(primitive_value) File "/home/maruthi/.virtualenvs/dmango/lib/python3.6/site-packages/rest_framework/fields.py", line 535, in run_validation value = self.to_internal_value(data) File "/home/maruthi/.virtualenvs/dmango/lib/python3.6/site-packages/rest_framework/fields.py", line 1667, in to_internal_value return self.run_child_validation(data) File "/home/maruthi/.virtualenvs/dmango/lib/python3.6/site-packages/rest_framework/fields.py", line 1681, in run_child_validation result.append(self.child.run_validation(item)) File "/home/maruthi/.virtualenvs/dmango/lib/python3.6/site-packages/rest_framework/fields.py", line 536, in run_validation self.run_validators(value) File "/home/maruthi/.virtualenvs/dmango/lib/python3.6/site-packages/rest_framework/fields.py", line 550, in run_validators validator(value) File "/home/maruthi/.virtualenvs/dmango/lib/python3.6/site-packages/rest_framework/validators.py", line 79, in __call__ queryset = self.filter_queryset(value, queryset) File "/home/maruthi/.virtualenvs/dmango/lib/python3.6/site-packages/rest_framework/validators.py", line 66, in filter_queryset return qs_filter(queryset, **filter_kwargs) File "/home/maruthi/.virtualenvs/dmango/lib/python3.6/site-packages/rest_framework/validators.py", line 31, in qs_filter return queryset.filter(**kwargs) File "/home/maruthi/.virtualenvs/dmango/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs) File "/home/maruthi/.virtualenvs/dmango/lib/python3.6/site-packages/django/db/models/query.py", line 844, in filter return self._filter_or_exclude(False, *args, **kwargs) File "/home/maruthi/.virtualenvs/dmango/lib/python3.6/site-packages/polymorphic/query.py", line 157, in _filter_or_exclude return super(PolymorphicQuerySet, self)._filter_or_exclude(negate, *(list(q_objects) + additional_args), **kwargs) File "/home/maruthi/.virtualenvs/dmango/lib/python3.6/site-packages/django/db/models/query.py", line 862, in _filter_or_exclude clone.query.add_q(Q(*args, **kwargs)) File "/home/maruthi/.virtualenvs/dmango/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1263, in add_q clause, _ = self._add_q(q_object, self.used_aliases) File "/home/maruthi/.virtualenvs/dmango/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1287, in _add_q split_subq=split_subq, File "/home/maruthi/.virtualenvs/dmango/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1164, in build_filter lookups, parts, reffed_expression = self.solve_lookup_type(arg) File "/home/maruthi/.virtualenvs/dmango/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1028, in solve_lookup_type _, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta()) File "/home/maruthi/.virtualenvs/dmango/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1389, in names_to_path "Choices are: %s" % (name, ", ".join(available))) django.core.exceptions.FieldError: Cannot resolve keyword '' into field. Choices are: abusable_tags, attribution_text, author, author_id, author_uuid, concept_uuid, created_at, deleted, difficulty_level, document_id, drive_url, estimated_reading_duration, file, file_type, modified_at, polymorphic_ctype, polymorphic_ctype_id, published, question_set_elements, questions, resource_type, source, subject_names, subject_uuids, target_assessment_types, target_exams, text_html, title_external, title_internal, topic_names, topic_uuids, uuid
I found why I am getting this error.
I have field subject_uuids = ArrayField(models.UUIDField(unique=True), null=True, blank=True)
and when Passing data as {"subject_uuids":["ac711a86-de89-4c2b-a571-774f1e227574"]}
getting error. I don't know why.
Thanks in advance.