作者:手机用户2502920645 | 来源:互联网 | 2023-07-15 13:28
I've been trying to store an array of integers in a field of a Django model. Based on this reply, I've been trying to do so using a CommaSeparatedIntegerField, however this has proved less intuitive than the name would imply.
我一直在尝试在Django模型的字段中存储一个整数数组。根据这个回复,我一直在尝试使用CommaSeparatedIntegerField这样做,但事实证明这不如名称所暗示的那么直观。
If I have a comma-separated list of integers (list = [12,23,31])
, and I store it in a CommaSeparatedIntegerField, it comes back as a string (retrieved_list outputs u'[1,2,3]')
. I cannot simply retrieve my integers : for instance, int(retrieved_list[1])
outputs 1 whereas list[1]
would output 23.
如果我有一个以逗号分隔的整数列表(list = [12,23,31]),并将其存储在CommaSeparatedIntegerField中,它将以字符串形式返回(retrieve_list输出u'[1,2,3]') 。我不能简单地检索我的整数:例如,int(retrieve_list [1])输出1而list [1]输出23。
So, do I have to do the parsing by hand, or is there any other solution? And how exactly does a CommaSeparatedIntegerField differs from a CharField? Seems to me like they behave pretty much the same...
那么,我是否必须手动解析,还是有其他解决方案?以及CommaSeparatedIntegerField与CharField的区别究竟如何?在我看来,他们表现得非常相似......
2 个解决方案