像django的orm里定义model 时field的顺序是怎么保证的呢?

2011-11-12 10:51:37 +08:00
 args
类似这样

class Person(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)


按理说这样定义的属性都会到Person的 __dict__ 对象里,这是一个字典,顺序完全没有保证的啊。不知道顺序的话,后面的插入之类的sql语句怎么实现?

有研究过的人进来说一说嘛~
5210 次点击
所在节点    Python
3 条回复
dreamersdw
2011-11-12 10:58:46 +08:00
INSERT INTO Persons (last_name, first_name) VALUES ('Tom', 'Wilson')
iiduce
2011-11-12 11:16:58 +08:00
The dict is a sorteddict,which presents the items in the order they are added.

https://code.djangoproject.com/wiki/SortedDict
kaiix
2011-11-12 11:39:20 +08:00
sorteddict python的dict是不保序的 所以django用了一个list来保存顺序 见django.utils.datastructure

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/21277

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX