django.db.utils.IntegrityError: (1048, “Column ‘spu_id’ cannot be null”) About RESTframework using serializer error…

ERROR exception 132 Internal Server Error: /meiduo_admin/goods/specs/
Traceback (most recent call last):
File "D:\python_learn\meiduo_project\env\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "D:\python_learn\meiduo_project\env\lib\site-packages\django\db\backends\mysql\base.py", line 101, in execute
return self.cursor.execute(query, args)
File "D:\python_learn\meiduo_project\env\lib\site-packages\pymysql\cursors.py", line 170, in execute
result = self._query(query)
File "D:\python_learn\meiduo_project\env\lib\site-packages\pymysql\cursors.py", line 328, in _query
conn.query(q)
File "D:\python_learn\meiduo_project\env\lib\site-packages\pymysql\connections.py", line 517, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "D:\python_learn\meiduo_project\env\lib\site-packages\pymysql\connections.py", line 732, in _read_query_result
result.read()
File "D:\python_learn\meiduo_project\env\lib\site-packages\pymysql\connections.py", line 1075, in read
first_packet = self.connection._read_packet()
File "D:\python_learn\meiduo_project\env\lib\site-packages\pymysql\connections.py", line 684, in _read_packet
packet.check_error()
File "D:\python_learn\meiduo_project\env\lib\site-packages\pymysql\protocol.py", line 220, in check_error
err.raise_mysql_exception(self._data)
File "D:\python_learn\meiduo_project\env\lib\site-packages\pymysql\err.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_learn\meiduo_project\env\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
    response = get_response(request)
  File "D:\python_learn\meiduo_project\env\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "D:\python_learn\meiduo_project\env\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "D:\python_learn\meiduo_project\env\lib\site-packages\django\views\decorators\csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "D:\python_learn\meiduo_project\env\lib\site-packages\rest_framework\viewsets.py", line 103, in view
    return self.dispatch(request, *args, **kwargs)
  File "D:\python_learn\meiduo_project\env\lib\site-packages\rest_framework\views.py", line 483, in dispatch
    response = self.handle_exception(exc)
  File "D:\python_learn\meiduo_project\env\lib\site-packages\rest_framework\views.py", line 443, in handle_exception
    self.raise_uncaught_exception(exc)
  File "D:\python_learn\meiduo_project\env\lib\site-packages\rest_framework\views.py", line 480, in dispatch
    response = handler(request, *args, **kwargs)
  File "D:\python_learn\meiduo_project\env\lib\site-packages\rest_framework\mixins.py", line 21, in create
    self.perform_create(serializer)
  File "D:\python_learn\meiduo_project\env\lib\site-packages\rest_framework\mixins.py", line 26, in perform_create
    serializer.save()
  File "D:\python_learn\meiduo_project\env\lib\site-packages\rest_framework\serializers.py", line 214, in save
    self.instance = self.create(validated_data)
  File "D:\python_learn\meiduo_project\env\lib\site-packages\rest_framework\serializers.py", line 940, in create
    instance = ModelClass.objects.create(**validated_data)
  File "D:\python_learn\meiduo_project\env\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "D:\python_learn\meiduo_project\env\lib\site-packages\django\db\models\query.py", line 394, in create
    obj.save(force_insert=True, using=self.db)
  File "D:\python_learn\meiduo_project\env\lib\site-packages\django\db\models\base.py", line 807, in save
    self.save_base(using=using, force_insert=force_insert,
  File "D:\python_learn\meiduo_project\env\lib\site-packages\django\db\models\base.py", line 838, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "D:\python_learn\meiduo_project\env\lib\site-packages\django\db\models\base.py", line 924, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "D:\python_learn\meiduo_project\env\lib\site-packages\django\db\models\base.py", line 962, in _do_insert
    return manager._insert([self], fields=fields, return_id=update_pk,
  File "D:\python_learn\meiduo_project\env\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "D:\python_learn\meiduo_project\env\lib\site-packages\django\db\models\query.py", line 1076, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "D:\python_learn\meiduo_project\env\lib\site-packages\django\db\models\sql\compiler.py", line 1112, in execute_sql
    cursor.execute(sql, params)
  File "D:\python_learn\meiduo_project\env\lib\site-packages\django\db\backends\utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "D:\python_learn\meiduo_project\env\lib\site-packages\django\db\backends\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "D:\python_learn\meiduo_project\env\lib\site-packages\django\db\utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "D:\python_learn\meiduo_project\env\lib\site-packages\django\utils\six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "D:\python_learn\meiduo_project\env\lib\site-packages\django\db\backends\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "D:\python_learn\meiduo_project\env\lib\site-packages\django\db\backends\mysql\base.py", line 101, in execute
    return self.cursor.execute(query, args)
  File "D:\python_learn\meiduo_project\env\lib\site-packages\pymysql\cursors.py", line 170, in execute
    result = self._query(query)
  File "D:\python_learn\meiduo_project\env\lib\site-packages\pymysql\cursors.py", line 328, in _query
    conn.query(q)
  File "D:\python_learn\meiduo_project\env\lib\site-packages\pymysql\connections.py", line 517, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "D:\python_learn\meiduo_project\env\lib\site-packages\pymysql\connections.py", line 732, in _read_query_result
    result.read()
  File "D:\python_learn\meiduo_project\env\lib\site-packages\pymysql\connections.py", line 1075, in read
    first_packet = self.connection._read_packet()
  File "D:\python_learn\meiduo_project\env\lib\site-packages\pymysql\connections.py", line 684, in _read_packet
    packet.check_error()
  File "D:\python_learn\meiduo_project\env\lib\site-packages\pymysql\protocol.py", line 220, in check_error
    err.raise_mysql_exception(self._data)
  File "D:\python_learn\meiduo_project\env\lib\site-packages\pymysql\err.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. According to the prompts, first query whether the front-end request data is incorrect:

No errors were found, and the returned request.data contained spu_id and name values.

2. But the error is displayed

"Column 'spu_id' cannot be null", and an error occurred during the saving process of pymysql, so it may be because the spu_id attribute is not defined in the serializer;<br>The original serialization code is as follows:
class SPUSpecsViewSerializer(serializers.ModelSerializer):

    spu = serializers.StringRelatedField(read_only=True)


    class Meta:
        model = SPUSpecification
        fields = ('id', 'name', 'spu_id', 'spu')

3. But when looking in the models model class, we found that the original model class object is not in the spu_id field.

class SPUSpecification(BaseModel):
    """Product SPU Specifications"""
    spu = models.ForeignKey(SPU, on_delete=models.CASCADE, related_name='specs', verbose_name='product SPU')
    name = models.CharField(max_length=20, verbose_name='Specification name')

    class Meta:
        db_table = 'tb_spu_specification'
        verbose_name = 'Product SPU specifications'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '%s: %s' % (self.spu.name, self.name)

4. So it is speculated that it may be because the spu_id field is not defined in the serializer, so it cannot correspond to the information in the table, so the definition of spu_id is added to the serializer:

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.Problem solving