I have a DB schema that I inherited from some legacy code. Now we intend to use Django to mirror tables in models. This means that I cannot create tables using django models. The table layout looks like this: -
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 created for this circuit looks like this: -
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 line into this, like this: -
StudentProfile.objects.insert(brothername="Bro")
The row is correctly inserted. But I have no way to extract the id field. It appears that since this field is not AutoField
, django does not copy the identifier that MySql automatically generates. What can I do to get the id
value with which the string was created? I checked ( https://code.djangoproject.com/ticket/8799#comment:1 ) and it looks by design. But I still need a way to get to id
and reflect it in the StudentProfile
model. What can I do?
One solution might be to override the default save()
value and explicitly provide id
in the save()
model. But is that right? The database (this is MySql) provides a way to generate an identifier, and I should be able to use this fact to my advantage, and not support the operation of the counter by the application. Any clues?
source share