不可散列的类型:'ReturnDict'drf django

时间:2020-03-16 05:04:00

标签: django python-3.x django-rest-framework

我正在尝试添加但出​​现此错误:

unhashable type: 'ReturnDict'

models.py:

class SumInsuredEntity(UUIDBase):
      field_name = CICharField(max_length=255,null=False,blank=False,verbose_name=_("Field Name"), unique=True)
      is_balance = models.ForeignKey(ValidChoice, to_field="key", db_column="is_balance", related_name="is_balance", null=True, blank=True, verbose_name=_("Is Balance"),default='no', on_delete=models.DO_NOTHING) 
      is_thresold = models.ForeignKey(ValidChoice, to_field="key", db_column="is_thresold", related_name="is_thresold", null=True, blank=True, verbose_name=_("Is Thresold"),default='no', on_delete=models.DO_NOTHING)
      status = models.SmallIntegerField(_("Status:1 for Active; 0:InActive"), default=1)
     class Meta:
          db_table = "tpa_master_sieav"

     def __str__(self):
        return str(self.uid)

Views.py:

@api_view(['POST'])
def sumInsuredEntityAdd(request):
'''
sumInsuredEntity Create Api
'''
data= decode_data(request.data.copy())
serializer_obj = SumInsuredEntitySerializer(data=data)
if serializer_obj.is_valid(raise_exception=True):
    try:
        sumInsured_save = serializer_obj.save()
        return CustomeResponse(request=request, comment=SUM_INSURED_ENTITY_ADDED_SUCCESSFULLY,data=json.dumps({serializer_obj.data}, cls=UUIDEncoder), message= SUM_INSURED_ENTITY_ADDED_SUCCESSFULLY, status=status.HTTP_200_OK)
    except Exception as e:
        print(e)
        return CustomeResponse(request=request, log_data=json.dumps(str(e), cls=UUIDEncoder), comment=COULD_NOT_SAVE_SUM_INSURED_ENTITY, message=COULD_NOT_SAVE_SUM_INSURED_ENTITY, data=json.dumps({}, cls=UUIDEncoder), status=status.HTTP_400_BAD_REQUEST, validate_errors=1)
else:
    return CustomeResponse(request=request, comment=FIELDS_NOT_VALID, message=FIELDS_NOT_VALID, data=json.dumps({}, cls=UUIDEncoder), status=status.HTTP_400_BAD_REQUEST, validate_errors=1)

它工作正常,但在尝试转储serializer_obj.data时显示错误

1 个答案:

答案 0 :(得分:1)

data=json.dumps({serializer_obj.data}, cls=UUIDEncoder)

应更改为

data=json.dumps(serializer_obj.data, cls=UUIDEncoder)

还有一件事。不要在语句中使用serializer_obj.is_valid(raise_exception=True):,因为else不会被执行