Storing A Binary Hash Value In A Django Model Field
Solution 1:
Starting with 1.6, Django has BinaryField
allowing to store raw binary data. However, for hashes and other values up to 128 bits it's more efficient (at least with the PostgreSQL backend) to use UUIDField
available in Django 1.8+.
Solution 2:
I'm assuming if you were writing raw SQL you'd be using a Postgres bytea or a MySQL VARBINARY. There's a ticket with a patch (marked "needs testing") that purportedly makes a field like this (Ticket 2417: Support for binary type fields (aka: bytea in postgres and VARBINARY in mysql)).
Otherwise, you could probably try your hand at writing a custom field type.
Solution 3:
"I have a twenty byte hex hash that I would like to store in a django model."
Django does this. They use hex digests, which are -- technically -- strings. Not bytes.
Do not use someHash.digest()
-- you get bytes, which you cannot easily store.
Use someHash.hexdigest()
-- you get a string, which you can easily store.
Edit -- The code is nearly identical.
Solution 4:
You could also write your own custom Model Manager that does the escaping and unescaping for you.
Solution 5:
If this issue is still of interest, Disqus' django-bitfield
fits the bill:
https://github.com/disqus/django-bitfield
... the example code on GitHub is a little confusing at first w/r/t the modules' actual function, because of the asinine variable names -- generally I am hardly the sort of person with either the wherewithal or the high ground to take someone elses' goofy identifiers to task... but flaggy_foo
?? Srsly, U guys.
If that project isn't to your taste, and you're on Postgres, you have a lot of excellent options as many people have written and released code for an assortment of Django fields that take advantage of Postgres' native type. Here's an hstore
model field:
https://github.com/jordanm/django-hstore -- I have used this and it works well.
Here's a full-text search implementation that uses Postgres' termvector types:
https://github.com/aino/django-pgindex
And while I cannot vouch for this specific project, there are Django bytea
fields as well:
Post a Comment for "Storing A Binary Hash Value In A Django Model Field"