how to filter with BinaryJSONField?

34 views
Skip to first unread message

Bin Lyu

unread,
Nov 18, 2020, 8:55:20 PM11/18/20
to peewee-orm

class Modex(baseModal):

    sdgdList = BinaryJSONField(verbose_name="sdgdList",null=True,)


sdgdList Data:

[{"x": "1", "cgs": "11.00", "gbxz": "A", "gdmc": "aaa", "zjqk": "0.48", "zzgs": "0.18", "zjqkCss": "red", "gdblockid": "10",}, {"x": "2", "cgs": "10.06", "gbxz": "A", "gdmc": "bbb", "zjqk": "0.32", "zzgs": "0.17", "zjqkCss": "red", "gdblockid": "10", },{"x": "2", "cgs": "10.06", "gbxz": "A", "gdmc": "aaa", "zjqk": "0.32", "zzgs": "0.17", "zjqkCss": "red", "gdblockid": "10", }]

how to filter 'gdmc' == 'aaa' then result:

[{"x": "1", "cgs": "11.00", "gbxz": "A", "gdmc": "aaa", "zjqk": "0.48", "zzgs": "0.18", "zjqkCss": "red", "gdblockid": "10",},{"x": "2", "cgs": "10.06", "gbxz": "A", "gdmc": "aaa", "zjqk": "0.32", "zzgs": "0.17", "zjqkCss": "red", "gdblockid": "10", }]


Charles Leifer

unread,
Nov 18, 2020, 8:57:39 PM11/18/20
to peewe...@googlegroups.com
I attempted to answer your question here: https://github.com/coleifer/peewee/issues/2300#issuecomment-729706157

Basically:

SomeModel.select().where(SomeModel.sdgdList['gdmc'] == 'aaa')

--
You received this message because you are subscribed to the Google Groups "peewee-orm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to peewee-orm+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/peewee-orm/22431cdd-4a10-4085-b4de-3f8273d2c25cn%40googlegroups.com.

Bin Lyu

unread,
Nov 18, 2020, 9:08:34 PM11/18/20
to peewee-orm
SomeModel.select().where(SomeModel.sdgdList['gdmc'] == 'aaa')
filter result is None!
Have you tested it?
SomeModel.select().where(SomeModel.sdgdList['gdmc'] == 'aaa') 
Apply to Data 
dict --> { 'gdmc': {'bar': ['i2']} }
List did't work-------------------
list --> [{"x": "1", "cgs": "11.00", "gbxz": "A", "gdmc": "aaa", "zjqk": "0.48", "zzgs": "0.18", "zjqkCss": "red", "gdblockid": "10",}, {"x": "2", "cgs": "10.06", "gbxz": "A", "gdmc": "bbb", "zjqk": "0.32", "zzgs": "0.17", "zjqkCss": "red", "gdblockid": "10", },]

Charles Leifer

unread,
Nov 18, 2020, 10:11:37 PM11/18/20
to peewe...@googlegroups.com
I didn't understand that the data was stored in lists. In that case I'm not too sure what the best way would be.

Bin Lyu

unread,
Nov 19, 2020, 1:18:59 AM11/19/20
to peewee-orm
Thank you for your reply.
in mongo i using args : {'sdgdList.gdmc':name} 
  Is there a similar operation method for peewee?

Charles Leifer

unread,
Nov 19, 2020, 8:36:53 AM11/19/20
to peewe...@googlegroups.com
Did you also try:

SomeModel.select().where(SomeModel.sdgdList.contains({'gdmc': 'aaa'}))

Bin Lyu

unread,
Nov 19, 2020, 9:06:30 AM11/19/20
to peewee-orm
Yes, I have queried and the return is empty.

Charles Leifer

unread,
Nov 19, 2020, 10:14:24 AM11/19/20
to peewe...@googlegroups.com
Ah, I just tested locally. Since your data is in a list, you just need to put the data inside a list that you're trying to match.

So this should work:

search = [{'gdmc': 'aaa'}]
SomeModel.select().where(SomeModel.sdgdList.contains(search))

Bin Lyu

unread,
Nov 19, 2020, 8:14:40 PM11/19/20
to peewee-orm
yes, So useful, i appreciate it very much.
Reply all
Reply to author
Forward
0 new messages