简体   繁体   中英

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. 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.

I'm using Django 2.1.5 and 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]" . 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. However, attempts to deliver it in that form in a JsonResponse result in it being returned in the string-ified form:

{
    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:

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.

deStringify is:

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.

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. 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.

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:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM