I have a DB Schema that I have inherited from some legacy code. Now we intend to use Django to reflect the tables into models. This means I cannot have the tables be generated using the django models. The table schema looks like so:-
我有一个DB Schema,我从一些遗留代码继承。现在我们打算使用Django将表格反映到模型中。这意味着我不能使用django模型生成表。表模式如下所示: -
mysql> desc student_profile;
+-------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| joining_on | datetime | YES | | NULL | |
| brothername | varchar(45) | YES | | NULL | |
+-------------+-------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)
Now the Django model that I have created for this schema looks like this:-
现在我为这个模式创建的Django模型如下所示: -
class StudentProfile(models.Model):
id=models.IntegerField(primary_key=True)
joining_on=models.DateField()
brothername=models.CharField(max_length=45)
class Meta:
db_table="student_profile"
When I insert a row into this, like so:-
当我在其中插入一行时,如下所示: -
StudentProfile.objects.insert(brothername="Bro")
The row is correctly inserted. But I do not have a way to extract the id field. It appears that since this field is not an AutoField
, django does not copy over the id that was automatically generated by MySql. What can I do to obtain the id
value with which the row was created? I checked (https://code.djangoproject.com/ticket/8799#comment:1), and this appears to be by design. But I still need a way to get to the id
and have it be reflected in the StudentProfile
model. What can I do?
该行已正确插入。但我没有办法提取id字段。看来,由于此字段不是AutoField,因此django不会复制MySql自动生成的id。我该怎么做才能获得创建行的id值?我检查了(https://code.djangoproject.com/ticket/8799#comment:1),这似乎是设计的。但我仍然需要一种方法来获取id并将其反映在StudentProfile模型中。我能做什么?
One solution could be to override the default save()
and explicitly provide the id
into the save()
of the model. But is that the right way? The database (it is MySql) provides a way to generate the id, and I must be able to use this fact to my advantage rather than having to maintain a running counter by the application. Any hints?
一种解决方案可能是覆盖默认的save()并明确地将id提供给模型的save()。但这是正确的方法吗?数据库(它是MySql)提供了一种生成id的方法,我必须能够利用这个事实,而不是必须通过应用程序维护一个运行计数器。任何提示?
1 个解决方案
#1
6
You can skip defining the id
column altogether, ie:
您可以跳过完全定义id列,即:
class StudentProfile(models.Model):
# id=models.IntegerField(primary_key=True)
joining_on=models.DateField()
brothername=models.CharField(max_length=45)
class Meta:
db_table="student_profile"
Django will create or use an autoincrement column named id
by default, which is the same as your legacy column.
Django默认会创建或使用名为id的自动增量列,这与您的旧列相同。
If the column name was different, you could use an AutoField
with a different name, such as:
如果列名称不同,则可以使用具有不同名称的AutoField,例如:
my_id = models.AutoField(primary_key=True)
#1
6
You can skip defining the id
column altogether, ie:
您可以跳过完全定义id列,即:
class StudentProfile(models.Model):
# id=models.IntegerField(primary_key=True)
joining_on=models.DateField()
brothername=models.CharField(max_length=45)
class Meta:
db_table="student_profile"
Django will create or use an autoincrement column named id
by default, which is the same as your legacy column.
Django默认会创建或使用名为id的自动增量列,这与您的旧列相同。
If the column name was different, you could use an AutoField
with a different name, such as:
如果列名称不同,则可以使用具有不同名称的AutoField,例如:
my_id = models.AutoField(primary_key=True)