简体   繁体   中英

select distinct values from JSON using django-mysql

I'm using this library and my model looks like this:

class PhoneTest(Model):
    data = JSONField()

My JSON obj looks something like this (in a real obj there are way more fields):

{ "deviceStatus": true, "officerCode": 123456, "imei": 123456789123456 }

For instance, I want to get a list of all officerCodes. How do I do that ? All I've tried so far has not worked. For example this did not:

tests = PhoneTests.objects.all()
tests.distinct('data__mOfficerCode')

It gives me the following error:

NotSupportedError: DISTINCT ON fields is not supported by this database backend

But it's because I'm using this new library, not the native django mysql backend. What are possible workarounds?

I would greatly appreciate any help.

您可以使用values_list方法

PhoneTests.objects.all().values_list('data__mOfficerCode').distinct()

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