ERROR exception 132 Internal Server Error: /meiduo_admin/goods/specs/ Traceback (most recent call last): File "D:python_learnmeiduo_projectenvlibsite-packagesdjangodbackendsutils.py", line 64, in execute return self.cursor.execute(sql, params) File "D:python_learnmeiduo_projectenvlibsite-packagesdjangodbackendsmysqlase.py", line 101, in execute return self.cursor.execute(query, args) File "D:python_learnmeiduo_projectenvlibsite-packagespymysqlcursors.py", line 170, in execute result = self._query(query) File "D:python_learnmeiduo_projectenvlibsite-packagespymysqlcursors.py", line 328, in _query conn.query(q) File "D:python_learnmeiduo_projectenvlibsite-packagespymysqlconnections.py", line 517, in query self._affected_rows = self._read_query_result(unbuffered=unbuffered) File "D:python_learnmeiduo_projectenvlibsite-packagespymysqlconnections.py", line 732, in _read_query_result result.read() File "D:python_learnmeiduo_projectenvlibsite-packagespymysqlconnections.py", line 1075, in read first_packet = self.connection._read_packet() File "D:python_learnmeiduo_projectenvlibsite-packagespymysqlconnections.py", line 684, in _read_packet packet.check_error() File "D:python_learnmeiduo_projectenvlibsite-packagespymysqlprotocol.py", line 220, in check_error err.raise_mysql_exception(self._data) File "D:python_learnmeiduo_projectenvlibsite-packagespymysqlerr.py", line 109, in raise_mysql_exception raise errorclass(errno, errval) pymysql.err.IntegrityError: (1048, "Column 'spu_id' cannot be null") The above exception was the direct cause of the following exception: Traceback (most recent call last): File "D:python_learnmeiduo_projectenvlibsite-packagesdjangocorehandlersexception.py", line 41, in inner response = get_response(request) File "D:python_learnmeiduo_projectenvlibsite-packagesdjangocorehandlersase.py", line 187, in _get_response response = self.process_exception_by_middleware(e, request) File "D:python_learnmeiduo_projectenvlibsite-packagesdjangocorehandlersase.py", line 185, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "D:python_learnmeiduo_projectenvlibsite-packagesdjangoviewsdecoratorscsrf.py", line 58, in wrapped_view return view_func(*args, **kwargs) File "D:python_learnmeiduo_projectenvlibsite-packages est_frameworkviewsets.py", line 103, in view return self.dispatch(request, *args, **kwargs) File "D:python_learnmeiduo_projectenvlibsite-packages est_frameworkviews.py", line 483, in dispatch response = self.handle_exception(exc) File "D:python_learnmeiduo_projectenvlibsite-packages est_frameworkviews.py", line 443, in handle_exception self.raise_uncaught_exception(exc) File "D:python_learnmeiduo_projectenvlibsite-packages est_frameworkviews.py", line 480, in dispatch response = handler(request, *args, **kwargs) File "D:python_learnmeiduo_projectenvlibsite-packages est_frameworkmixins.py", line 21, in create self.perform_create(serializer) File "D:python_learnmeiduo_projectenvlibsite-packages est_frameworkmixins.py", line 26, in perform_create serializer.save() File "D:python_learnmeiduo_projectenvlibsite-packages est_frameworkserializers.py", line 214, in save self.instance = self.create(validated_data) File "D:python_learnmeiduo_projectenvlibsite-packages est_frameworkserializers.py", line 940, in create instance = ModelClass.objects.create(**validated_data) File "D:python_learnmeiduo_projectenvlibsite-packagesdjangodbmodelsmanager.py", line 85, in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs) File "D:python_learnmeiduo_projectenvlibsite-packagesdjangodbmodelsquery.py", line 394, in create obj.save(force_insert=True, using=self.db) File "D:python_learnmeiduo_projectenvlibsite-packagesdjangodbmodelsase.py", line 807, in save self.save_base(using=using, force_insert=force_insert, File "D:python_learnmeiduo_projectenvlibsite-packagesdjangodbmodelsase.py", line 838, in save_base updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields) File "D:python_learnmeiduo_projectenvlibsite-packagesdjangodbmodelsase.py", line 924, in _save_table result = self._do_insert(cls._base_manager, using, fields, update_pk, raw) File "D:python_learnmeiduo_projectenvlibsite-packagesdjangodbmodelsase.py", line 962, in _do_insert return manager._insert([self], fields=fields, return_id=update_pk, File "D:python_learnmeiduo_projectenvlibsite-packagesdjangodbmodelsmanager.py", line 85, in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs) File "D:python_learnmeiduo_projectenvlibsite-packagesdjangodbmodelsquery.py", line 1076, in _insert return query.get_compiler(using=using).execute_sql(return_id) File "D:python_learnmeiduo_projectenvlibsite-packagesdjangodbmodelssqlcompiler.py", line 1112, in execute_sql cursor.execute(sql, params) File "D:python_learnmeiduo_projectenvlibsite-packagesdjangodbackendsutils.py", line 79, in execute return super(CursorDebugWrapper, self).execute(sql, params) File "D:python_learnmeiduo_projectenvlibsite-packagesdjangodbackendsutils.py", line 64, in execute return self.cursor.execute(sql, params) File "D:python_learnmeiduo_projectenvlibsite-packagesdjangodbutils.py", line 94, in __exit__ six.reraise(dj_exc_type, dj_exc_value, traceback) File "D:python_learnmeiduo_projectenvlibsite-packagesdjangoutilssix.py", line 685, in reraise raise value.with_traceback(tb) File "D:python_learnmeiduo_projectenvlibsite-packagesdjangodbackendsutils.py", line 64, in execute return self.cursor.execute(sql, params) File "D:python_learnmeiduo_projectenvlibsite-packagesdjangodbackendsmysqlase.py", line 101, in execute return self.cursor.execute(query, args) File "D:python_learnmeiduo_projectenvlibsite-packagespymysqlcursors.py", line 170, in execute result = self._query(query) File "D:python_learnmeiduo_projectenvlibsite-packagespymysqlcursors.py", line 328, in _query conn.query(q) File "D:python_learnmeiduo_projectenvlibsite-packagespymysqlconnections.py", line 517, in query self._affected_rows = self._read_query_result(unbuffered=unbuffered) File "D:python_learnmeiduo_projectenvlibsite-packagespymysqlconnections.py", line 732, in _read_query_result result.read() File "D:python_learnmeiduo_projectenvlibsite-packagespymysqlconnections.py", line 1075, in read first_packet = self.connection._read_packet() File "D:python_learnmeiduo_projectenvlibsite-packagespymysqlconnections.py", line 684, in _read_packet packet.check_error() File "D:python_learnmeiduo_projectenvlibsite-packagespymysqlprotocol.py", line 220, in check_error err.raise_mysql_exception(self._data) File "D:python_learnmeiduo_projectenvlibsite-packagespymysqlerr.py", line 109, in raise_mysql_exception raise errorclass(errno, errval) django.db.utils.IntegrityError: (1048, "Column 'spu_id' cannot be null") ERROR basehttp 124 "POST /meiduo_admin/goods/specs/ HTTP/1.1" 500 28792
1.根据提示一开始查询是否是前端请求数据有误:
发现没有错误,返回的request.data中有spu_id和name值
2.但是报错显示
"Column 'spu_id' cannot be null",且是pymysql在保存过程中出现错误,所以可能是因为在serializer序列化器中的没有定义spu_id的属性;
原来的序列化是代码如下:
class SPUSpecsViewSerializer(serializers.ModelSerializer): spu = serializers.StringRelatedField(read_only=True) class Meta: model = SPUSpecification fields = ('id', 'name', 'spu_id', 'spu')
3.但是在models模型类中查看,发现原来模型类对象并不处在spu_id字段
class SPUSpecification(BaseModel): """商品SPU规格""" spu = models.ForeignKey(SPU, on_delete=models.CASCADE, related_name='specs', verbose_name='商品SPU') name = models.CharField(max_length=20, verbose_name='规格名称') class Meta: db_table = 'tb_spu_specification' verbose_name = '商品SPU规格' verbose_name_plural = verbose_name def __str__(self): return '%s: %s' % (self.spu.name, self.name)
4.所以推测可能是因为在序列化器中未定义spu_id字段,所以无法和表中信息相对应,所以在序列化器中加入spu_id的定义:
class SPUSpecsViewSerializer(serializers.ModelSerializer): spu = serializers.StringRelatedField(read_only=True) spu_id = serializers.IntegerField() class Meta: model = SPUSpecification fields = ('id', 'name', 'spu_id', 'spu')
5.问题解决