Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 使用auto_now_加载项Django保存模型实例将返回错误消息列“xxx”不能为空_Mysql_Django - Fatal编程技术网

Mysql 使用auto_now_加载项Django保存模型实例将返回错误消息列“xxx”不能为空

Mysql 使用auto_now_加载项Django保存模型实例将返回错误消息列“xxx”不能为空,mysql,django,Mysql,Django,我一定是做错了什么,否则这个错误对我来说毫无意义。我有一个对象位置: 这是我插入新位置记录或更新现有位置记录的代码: class LocationList(generics.ListCreateAPIView): queryset = Location.objects.all() serializer_class = LocationSerializer def post(self, request, *args, **kwargs): locatio

我一定是做错了什么,否则这个错误对我来说毫无意义。我有一个对象位置:

这是我插入新位置记录或更新现有位置记录的代码:

class LocationList(generics.ListCreateAPIView):
    queryset = Location.objects.all()
    serializer_class = LocationSerializer

    def post(self, request, *args, **kwargs):

        location_dict = request.data
        if 'location_id' not in location_dict:
            okStatus = status.HTTP_201_CREATED
        else:
            okStatus = status.HTTP_200_OK
        location = Location(**location_dict)
        location.save()
        return Response(LocationSerializer(location).data, status=okStatus)

插入工作正常,但每次更新时,我得到的错误列“created”不能为null。我的在线研究似乎向我指出了一个事实,那就是这是一个早已修复的bug。我希望更新能够通过,因为“created”字段被设置为auto_now_add,这意味着Django应该在插入时设置该字段一次,并将其保留在任何后续更新中。我不知道为什么Django试图在更新时将该列设置为null或任何其他值,因为我希望Django根本不会更新该列。我正在使用MySQL作为数据库。

我认为您的问题在创建列中,请尝试以下步骤:-

1在创建的列中添加null=True

2运行:$pythonselect version manage.py makemigrations appName或将其保留为空

3运行:$pythonV manage.py sqlmegrate aapName文件版本> 检查应用程序目录中的文件版本,按以下方式键入0001

4运行:$pythonV manage.py迁移appName


在这些步骤更新URDB之后,最后一步是从创建的列中删除null=True并启动项目

如果“location\u id”不在location\u dict:中,那么引号需要存在吗?我想是的,但我不认为这会导致错误,错误是在行位置抛出的。saveyou可以在代码中去掉它,而使用MySQL默认值。您可以编辑该列以获得NOW on insert的值,这样DB将处理它而不是您的代码。
class LocationList(generics.ListCreateAPIView):
    queryset = Location.objects.all()
    serializer_class = LocationSerializer

    def post(self, request, *args, **kwargs):

        location_dict = request.data
        if 'location_id' not in location_dict:
            okStatus = status.HTTP_201_CREATED
        else:
            okStatus = status.HTTP_200_OK
        location = Location(**location_dict)
        location.save()
        return Response(LocationSerializer(location).data, status=okStatus)