Skip to content
Advertisement

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)

Advertisement

Answer

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().iterator():
    try:
        json.loads(item.test_data)
    except ValueError:
        # is invalid JSON, process
        # …
        pass
Advertisement