IntegrityError:(1048," Column' user_dob'不能为null")

时间:2016-09-08 10:17:36

标签: python django django-rest-framework

我修改了序列化程序字段,如下所示

class UserSerializer(serializers.ModelSerializer):
    id = serializers.IntegerField(source='user_id')
    name = serializers.CharField(source='get_name')
    email = serializers.EmailField(source='user_email')
    dob = serializers.DateField(source='user_dob')
    contactNo = serializers.CharField(source='user_mobileno')
    image = serializers.CharField(source='user_image')
    bloodGroup = serializers.CharField(source='user_blood_group')
    location =     serializers.CharField(source='user_location_id.location_name')
    profession = serializers.CharField(source='user_profession')
    fbId = serializers.CharField(source='user_fb_id')
    userId = serializers.CharField(source='user_random_id')

    class Meta:
        model = UserInfo
        # fields = ['user_id','get_name']
        fields = ["id", "name", "email", "dob", "contactNo", "image", "bloodGroup", "location", "profession", "fbId", "userId"]

现在,当我使用以下代码形式创建一个创建请求时,views.py,

def create(self, request, *args, **kwargs):
        # partial = kwargs.pop('partial', True)
        serializer = self.get_serializer(data=request.data, partial=True)
        print("kwargs------------>",serializer)
        serializer.is_valid(raise_exception=True)
        self.perform_create(serializer)
        # headers = self.get_success_headers(serializer.data)
        response = {
                "status" : status.HTTP_201_CREATED,
                "message" : "User Created.",
                "response" : serializer.data
            }
        return Response(response)

它给了我以下错误

IntegrityError: (1048, "Column 'user_dob' cannot be null")

即使设置 partial = True ,它也无效。

PUT 请求也无法正常工作,它不会更新数据

def update(self, request, *args, **kwargs):
        instance = self.get_object()
        serializer = self.get_serializer(instance, data=request.data, partial=True)
        # serializer = self.get_serializer(instance, data=request.data)
        serializer.is_valid(raise_exception=True)
        self.perform_update(serializer)
        response = {
                "status" : status.HTTP_200_OK,
                "message" : "User Detail Updated.",
                "response" : serializer.data
            }
        return Response(response)

任何人都可以帮助我吗?

以下是我的邮件参数

enter image description here

1 个答案:

答案 0 :(得分:2)

由于您使用的是序列化程序,可以映射您的" user_dob"字段" dob"你应该根据序列号中提供的名称传递参数。