开发者

Django object's id isn't available after save

开发者 https://www.devze.com 2023-02-16 05:28 出处:网络
I have the following Bank model: from django.db import models class Bank(models.Model): id = models.BigIntegerField(primary_key=True)

I have the following Bank model:

from django.db import models

class Bank(models.Model):
    id = models.BigIntegerField(primary_key=True)
    name = models.CharField(unique=True, max_length=255)
    identi开发者_Go百科fier = models.CharField(unique=True, max_length=255)
    created_at = models.DateTimeField()
    updated_at = models.DateTimeField()
    class Meta:
        db_table = u'bank'
        app_name = 'mcif'

Now look at this:

>>> from mcif.models import *
>>> b = Bank()
>>> b.name = "wee"
>>> b.identifier = "wee"
>>> b.save()
>>> b.id
>>>

If I understand how Django works, that Bank object is supposed to get updated with the id of the saved record. Any idea why this isn't happening?


Its a problem in Django when dealing with BigInt as Autoincrementing PK. Maybe this code snippet helps you solve this issue. Its a makeshift solution

0

精彩评论

暂无评论...
验证码 换一张
取 消