简体   繁体   中英

django custom function for filter query

I've a field in my model called, test_data = models.TextField(...) , and the model is called MyOrm

and this test_data contains data which is actually string, some holds JSON data, and some reference to blob-url.

Now I'm trying to streamline my data. So I want to filter all the MyOrm object whose test_data ain't JSON.

I'm just storing/trying to store some meta-data along with url, then I will convert them.

Can anyone suggest me a way to do so??

pseudocode:

select all my-orm where is_not_json(my-orm.test-data)

There is no database function for this that I'm aware of. You can define your own, but then this is more database programming.

I think you have no other option than to enumerate over the MyOrm model objects, and check if you can JSON decode these, with:

import json

for item in MyOrm.objects.all():
    try:
        json.loads(item.test_data)
    except ValueError:
        # is invalid JSON, process
        # …
        pass

or if memory might be a problem, you can work with .iterator(…) [Django-doc] :

import json

for item in MyOrm.objects.all():
    try:
        json.loads(item.test_data)
    except ValueError:
        # is invalid JSON, process
        # …
        pass

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