Question

I have this model on a database which I'm only able to read from:

class Post(models.Model):
    title = models.CharField(max_length=60)
    body = models.TextField()
    created = models.DateTimeField(auto_now_add=True)

Both title and body are unicode texts stored on the database as base64 encoded strings, I wish to be able to filter posts according to title's context.

I've been reading an old post with someone said that since base64 does not align on byte boundaries, it is impossible to filter using query encoded text. I wonder if anyone encountered and found a solution for this kind of problem since that old question was asked.

Thanks

Was it helpful?

Solution

Are you using PostgreSQL? You can add an index that is "this field base64-decoded" (see http://www.postgresql.org/docs/9.1/static/functions-string.html for base64 functions and http://www.postgresql.org/docs/9.1/static/sql-createindex.html for creating indexes). Then, in a database shell, you can practice querying against the field.

Once you know that works, you can write queries in Django using either .extra() or .raw() that can use these kind of queries. Both of these options still return normal Django result sets and, given that you'll have indexed the field, will operate very quickly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top