热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用Django存储整数数组-StoringanarrayofintegerswithDjango

IvebeentryingtostoreanarrayofintegersinafieldofaDjangomodel.Basedonthisreply,I

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 个解决方案

#1


5  

The only difference between this field type and a CharField is that it validates that the data is in the proper format - only digits separated by ','. You can view the source for the class at http://docs.nullpobug.com/django/trunk/django.db.models.fields-pysrc.html#CommaSeparatedIntegerField (expand the '+').

此字段类型与CharField之间的唯一区别是它验证数据的格式是否正确 - 只有数字以','分隔。您可以在http://docs.nullpobug.com/django/trunk/django.db.models.fields-pysrc.html#CommaSeparatedIntegerField(展开'+')查看该类的源代码。

You can turn such a string into an actual Python list value (not array - list is the proper term for Python) by using eval.

您可以使用eval将这样的字符串转换为实际的Python列表值(不是数组 - 列表是Python的正确术语)。

>>> eval("[1,2,3,4]")
[1, 2, 3, 4]

EDIT: eval has safety concerns, however and a better method is to use literal_eval as suggest by Alvin in another answer.

编辑:eval有安全问题,但更好的方法是使用literal_eval作为Alvin在另一个答案中的建议。

#2


10  

Eval was accepted as answer above -- avoid the temptation it's just not safe

Eval被接受作为上面的答案 - 避免诱惑它只是不安全

See: Python: make eval safe

请参阅:Python:使eval安全

There is a literal_eval function that could be used the same way:

有一个literal_eval函数可以使用相同的方式:

>>> from ast import literal_eval
>>> literal_eval("[1,2,3,4]")
[1, 2, 3, 4]

推荐阅读
  • 十大经典排序算法动图演示+Python实现
    本文介绍了十大经典排序算法的原理、演示和Python实现。排序算法分为内部排序和外部排序,常见的内部排序算法有插入排序、希尔排序、选择排序、冒泡排序、归并排序、快速排序、堆排序、基数排序等。文章还解释了时间复杂度和稳定性的概念,并提供了相关的名词解释。 ... [详细]
  • Thisworkcameoutofthediscussioninhttps://github.com/typesafehub/config/issues/272 ... [详细]
  • 都说Python处理速度慢,为何月活7亿的 Instagram依然在使用Python?
    点击“Python编程与实战”,选择“置顶公众号”第一时间获取Python技术干货!来自|简书作者|我爱学python链接|https:www.jian ... [详细]
  • fzu 1715 Ball and Box n个不同的求放到m个不同的盒子中方法的个数
    1715BallandBoxAccept:120Submit:288TimeLimit:1000mSecMemoryLimit:32768KBProblem ... [详细]
  • C语言:(1)整数是没有小数部分的数字(2)int类型在计算机中以二进制补码形式储存。(3)早期的整形在内存中占2字节,现代计算机中大多占4字节,取&# ... [详细]
  • 题目大意:给定数字,将其转化为罗马数字的形式罗马数字其实只有IVXLCDM这几种形式,其余均为组合的,去百度了解一下就ok。所以首先想到的就是,将个、十、百、千位的 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • This article discusses the efficiency of using char str[] and char *str and whether there is any reason to prefer one over the other. It explains the difference between the two and provides an example to illustrate their usage. ... [详细]
  • 本文讨论了微软的STL容器类是否线程安全。根据MSDN的回答,STL容器类包括vector、deque、list、queue、stack、priority_queue、valarray、map、hash_map、multimap、hash_multimap、set、hash_set、multiset、hash_multiset、basic_string和bitset。对于单个对象来说,多个线程同时读取是安全的。但如果一个线程正在写入一个对象,那么所有的读写操作都需要进行同步。 ... [详细]
  • 本文介绍了Java中Currency类的getInstance()方法,该方法用于检索给定货币代码的该货币的实例。文章详细解释了方法的语法、参数、返回值和异常,并提供了一个示例程序来说明该方法的工作原理。 ... [详细]
  • 本文整理了315道Python基础题目及答案,帮助读者检验学习成果。文章介绍了学习Python的途径、Python与其他编程语言的对比、解释型和编译型编程语言的简述、Python解释器的种类和特点、位和字节的关系、以及至少5个PEP8规范。对于想要检验自己学习成果的读者,这些题目将是一个不错的选择。请注意,答案在视频中,本文不提供答案。 ... [详细]
  • 解决Sharepoint 2013运行状况分析出现的“一个或多个服务器未响应”问题的方法
    本文介绍了解决Sharepoint 2013运行状况分析中出现的“一个或多个服务器未响应”问题的方法。对于有高要求的客户来说,系统检测问题的存在是不可接受的。文章详细描述了解决该问题的步骤,包括删除服务器、处理分布式缓存留下的记录以及使用代码等方法。同时还提供了相关关键词和错误提示信息,以帮助读者更好地理解和解决该问题。 ... [详细]
  • ProblemDescriptionXiaoAlivesinavillage.Lastyearfloodrainedthevillage ... [详细]
author-avatar
手机用户2502920645
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有