更新时间:2023-12-04 12:22:40
From the Django docs (1, 2):
主键字段是只读的。如果更改现有对象上的主键的值然后保存,则将与旧对象一起创建一个新对象。
The primary key field is read-only. If you change the value of the primary key on an existing object and then save it, a new object will be created alongside the old one.
问题出在模型对象的类定义中。
将primary_key字段设置为 AutoField
后,问题就消失了。
我的新模型定义如下:
The issue was in the model object's class definition.
Once I set the primary_key field to an AutoField
, the issue went away.
My new model definition is as follows:
class Meal(models.Model):
mealid = models.AutoField(db_column='MealId', primary_key=True)
name = models.CharField(db_column='Name', max_length=45, blank=True)
description = models.CharField(db_column='Description', max_length=200, blank=True)
Django完美的自动生成功能!
An almost perfect auto-generation by Django!