[英]Storing and retrieving a list of integers in sqlite using django
I'm prototyping a JSON API in Django (final implementation will be something embedded such as Civet) in order to have something to test a higher level application against. 我要在Django中建立JSON API的原型(最终实现将是诸如Civet之类的嵌入式产品),以便针对更高的应用程序进行测试。 I need to persist data of the form:
我需要保留以下形式的数据:
{
someListOfIds: [1, 2, 7, 9]
}
The integers mean something to the hardware - they don't correspond to anything else in the db. 整数对硬件意味着某种意义-它们与db中的其他任何内容都不对应。
I'm using Django 2.1.5 and Python 3.5.2. 我正在使用Django 2.1.5和Python 3.5.2。 In my model,
someListOfIds
is currently defined as a CharField so the value is stored as the string "[1, 2, 7, 9]"
. 在我的模型中,
someListOfIds
当前定义为CharField,因此该值存储为字符串"[1, 2, 7, 9]"
someListOfIds
"[1, 2, 7, 9]"
。 When I retrieve rows from the db in a view, I pass that field through the .decode
method of a json.decoder.JSONDecoder()
, which seems to turn it back into a list of ints. 当我从视图中的db中检索行时,我将该字段通过
json.decoder.JSONDecoder()
的.decode
方法传递,这似乎将其转换为整数列表。 However, attempts to deliver it in that form in a JsonResponse
result in it being returned in the string-ified form: 但是,尝试以
JsonResponse
形式交付它会导致它以字符串形式返回:
{
someListOfIds: "[1, 2, 7, 9]"
}
NOTE: Following advice from here , I currently have the following view code to return just the content of the fields
property, discarding the extra pk
and model
properties that would otherwise be included: 注意:根据这里的建议,我目前拥有以下视图代码,仅返回
fields
属性的内容,而丢弃了本应包含的额外的pk
和model
属性:
cfgs = [Configuration.deStringify(x) for x in Configuration.objects.all()]
objs = serializers.serialize('python', cfgs)
cfgs = [d['fields'] for d in objs]
response = { "configurations": cfgs }
return JsonResponse(response)
As far as I can tell, it's the serialization to a python object that reintroduces the string-y-ness. 据我所知,是对python对象的序列化,重新引入了string-y-ness。
deStringify
is: deStringify
是:
def deStringify(self):
decoder=json.decoder.JSONDecoder()
self.someListOfIds = decoder.decode(self.someListOfIds)
return self
This is very much a throwaway piece of code - it just needs to serve up correctly structured data for a while. 这几乎是一堆废弃的代码-它只需要一段时间来提供正确结构化的数据。 A working solution, without making a fresh start and adding a dedicated REST framework is all I'm looking for.
我一直在寻找一个可行的解决方案,而无需重新开始并添加专用的REST框架。
While not describing the exact scenario, this hint from the docs made me suspect that my conversion from string-containing-list-of-ints to actual-list-of-ints was being undone when serializers.serialize(...)
subsequently read the property. 虽然没有描述确切的情况,但是文档中的此提示使我怀疑,当
serializers.serialize(...)
随后读取时,我从int包含字符串的int到int实际列表的转换被撤消了财产。 I now perform the transformation after extracting the fields
property from the python-serialized structure and my JsonResponse now correctly contains the intended list-of-ints. 现在,我从python序列化结构中提取了
fields
属性之后执行了转换,并且我的JsonResponse现在正确包含了所需的int列表。
It's seems like a lot of work and probably reflects me not "getting" the Django way of doing things, and it's certainly not pretty, but it does the job: 这似乎是一项艰巨的工作,可能反映出我没有“了解” Django的工作方式,虽然它虽然不漂亮,但却可以完成工作:
decoder=json.decoder.JSONDecoder()
all = Configuration.objects.all()
objs = serializers.serialize('python', all)
# Extract the interesting part of the structure
cfgs = [d['fields'] for d in objs]
for key in ['someListOfIds', 'someOtherListOfInts', 'finalListOfInts']:
[cfg.update({key: decoder.decode(cfg[key])}) for cfg in cfgs]
response = { "configurations": cfgs }
return JsonResponse(response)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.