2

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)
1
  • If you're open to a schema change to do this, maybe consider using a JSON field type instead of TextField? Commented Mar 10, 2022 at 20:42

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

3 Comments

object.all() maybe takes a ton of memory... the server i'm running it, it's a 4gb server... can you suggest some efficient way? And also provide some link to that database programming you mentioned?
@Danial: if memory is an issue, you can work with .iterator() to process the items in batches.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.