保存时查询集记录不会获取 id 吗?
在 Rails 中以及在 symfony 和 Doctrine 中,您可以保存一条记录,然后您将获得该记录的 id。例如:
b = Bank.new
b.save
#b.id now has a value
但在 Django 中,这显然不是它的工作方式:
>>> b = Bank()
>>> b.name = "Fred's Bank"
>>> b.identifier = "fred"
>>> b.save()
>>> b.id
>>> b.id.__class__
<type 'NoneType'>
>>>
如您所见,b.id
的计算结果为空。如果我想要的话,我必须返回并选择 id 吗?这将是一件很痛苦的事情,特别是对于有很多列的表。
In Rails, and in symfony with Doctrine, you can save a record and then you'll have the record's id. For example:
b = Bank.new
b.save
#b.id now has a value
But in Django that's apparently not how it works:
>>> b = Bank()
>>> b.name = "Fred's Bank"
>>> b.identifier = "fred"
>>> b.save()
>>> b.id
>>> b.id.__class__
<type 'NoneType'>
>>>
As you can see, b.id
evaluates to nothing. Do I have to go back and select the id if I want it? That would be a pain in the ass, especially for tables that have a lot of columns.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是它在 Django 中的工作原理。假设
Bank
是一个 Django 模型,它确实在保存时获得一个 ID,并且当前实例反映了这一点。我无法想象你做了什么来打破这一点。您需要发布银行模型的代码。That is how it works in Django. Assuming that
Bank
is a Django model, it does get an ID on save, and the current instance reflects that. I can't imagine what you have done to break that. You'll need to post the code of the Bank model.