in my assessment section model i have defined an foreign key like this:
assessment_id = models.ForeignKey(to=Assessment, on_delete=models.CASCADE,
related_name='section_assessment_mapping', null=False, blank=False,
help_text="The assessment associated with this section.")
this points to assessment model’s id whichis primary key.
in assessment section serializer i have defined the assessment id like this:
assessment_id = serializers.UUIDField(required=True)
this is the create method of the serializer:
def create(self, validated_data):
print('validdata',validated_data)
print(validated_data.get('assessment_id'))
section_obj = AssessmentSection.objects.create_with_defaults(**validated_data)
return section_obj
and this is the create api for assessment section:
def create(self, request, *args, **kwargs):
try:
serializer = AssessmentSectionSerializer(data=request.data)
if serializer.is_valid(raise_exception=True):
serializer.save()
return Response(data=serializer.data, status=status.HTTP_201_CREATED)
except ValidationError as e:
service_logger.error(str(e))
return Response({'error': True, 'message': e.args[0]}, status=status.HTTP_400_BAD_REQUEST)
except Exception as e:
service_logger.error(str(e))
raise StandardizedException(error_status=True, error_obj=e, status_code=status.HTTP_400_BAD_REQUEST)
now when i hit the API with payload containing assessment_id having some valid assessment id which is present in the DB it gives this error:
Cannot assign "UUID('7025deca-afb8-4ad3-93b8-6035599bcf6e')": "AssessmentSection.assessment_id" must be a "Assessment" instance."
}
I have tried several approaches but they are not working. I just need to store the assessment id in the table with section info.
Thank you in advance.
tried using source=assesment.id in the serializer field but didnt work.
darshan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.