Skip to content
Advertisement

Querying a Django model and comparing its fields for similarities or near duplicates

I have a model that records changes to other models in my database.

I would like to query for changes that only add a newline character — n.

My changes model looks like this:

JavaScript

Essentially, I want to find instances of Change where the difference between a value for a key in old_fields and new_fields is n.

Here’s what new_fields look like:

JavaScript

And similarly, old_fields:

JavaScript

Note that new_fields and old_fields are both JSONFields.

Ultimately, I want to remove the Change instances that only add n to the body.

I realize that I could do this by iterating over the queryset and looking for these discrepancies, but I’m wondering if there’s a more elegant solution.

Advertisement

Answer

You can use the __startswith (along with an F() expression) and __endswith filters to check if the old_fields body starts with the new_fields body and ends with a :

JavaScript

This would, however, still match if the old_fields body had any string between the end of the new_fields body and the ending line break (e.g. "Had a great time on Wednesday.aaaaaaaaaaaaaaaaaan"). To fix that, you can also annotate the queryset with a Length function and check if the old_fields body’s length is equal to Length("new_fields__body") + 1:

JavaScript

This should work!

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement