Issues saving hex format data to binaryfield

86 views
Skip to first unread message

Brian O'Connell

unread,
Feb 22, 2017, 1:08:35 PM2/22/17
to Django users
I have a project where I am saving RFID information (which comes in as hex strings) to a bytea object in my postgres database. I can retrieve this information without issue. I am struggling to overwrite that object with new data (For when someone loses their rfid and needs to replace it). I have been unable to find an example of someone saving a small amount of data to a binaryfield. Just lots of people saying don't save images or files to binary fields and then explaining how to do that. 

Can anyone help me with simply saving something to a binaryfield to update a bytea object in a PostgreSQL database?

Thanks,
Brian

Shawn Milochik

unread,
Feb 22, 2017, 2:10:33 PM2/22/17
to django...@googlegroups.com
Why not base64 encode it and just store it in a charfield? There doesn't seem to be any compelling reason to store it in binary.

ludovic coues

unread,
Feb 22, 2017, 2:19:14 PM2/22/17
to django...@googlegroups.com
Can't you use django's BinaryField ?

2017-02-22 20:09 GMT+01:00 Shawn Milochik <shawn...@gmail.com>:
> Why not base64 encode it and just store it in a charfield? There doesn't
> seem to be any compelling reason to store it in binary.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAOzwKwG9WD2g4nvjpnbA1Y%2BuKxLmShz%3D3woTAKG%2B3VvQroWs0g%40mail.gmail.com.
>
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42

Brian O'Connell

unread,
Feb 22, 2017, 5:47:55 PM2/22/17
to Django users
I have the model set up as a binaryfield. The problem is I have no idea how to save the information to it. I can retrieve information from it easy enough but if I try and enter in new information, saved in the same format as that it returns

Here's what I have in model.py

class Users(models.Model):
    ....
    rfid = models.BinaryField(editable = True, unique=True, blank=True, null=True)
    ....

    def set_rfid(self,rfid_new):
        print self.rfid # print the existing object data
        try:
            self.rfid = bytearray(rfid_new)
            print self.rfid
        except Exception as ex:
            template = "An exception of type {0} occured. Arguments:\n{1!r}"
            message = template.format(type(ex).__name__, ex.args)
            print message

It doesn't raise an exception but it also doesn't save it into the field. 
This is the print out (Standard PostgreSQL escape format):

E\xa343c5ea

E\x52577ade


Any help would be appreciated. 


Thanks,

Brian

Brian O'Connell

unread,
Feb 22, 2017, 5:55:42 PM2/22/17
to Django users, Sh...@milochik.com
The database is pre-existing and I'm creating a Django web interface for it. This object holds the 8-digit hex output of the unique mfr identifier of the MiFare rfids. There are remote terminals that interact with database through psycopg2 and have no issue with that field. 

I could change the format in the database and recode those devices but I'd rather just write the correct format to the database than go through the trouble of changing multiple aspects of the system. I'm approaching on that threshold of time spent trying to find this solution to time it would take to update the entire system but I would much rather prefer to know how to write the binaryfield in general anyways. 

ludovic coues

unread,
Feb 22, 2017, 8:14:04 PM2/22/17
to django...@googlegroups.com
As you have editable = True, I assume you have read the code a bit. I
personnaly stopped reading after RegisterLookupMixin. If looking at
the code don't give you a solution, I would try to fallback to raw
SQL. That might be a skill more useful than the ability to update
BinaryField :)
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/5d7b7d99-5416-4c9a-947a-10acd5fb37d0%40googlegroups.com.

Melvyn Sopacua

unread,
Feb 23, 2017, 4:48:37 AM2/23/17
to django...@googlegroups.com

On Wednesday 22 February 2017 14:47:55 Brian O'Connell wrote:

 

> def set_rfid(self,rfid_new):

> print self.rfid # print the existing object data

> try:

> self.rfid = bytearray(rfid_new)

> print self.rfid

> except Exception as ex:

> template = "An exception of type {0} occured.

> Arguments:\n{1!r}" message = template.format(type(ex).__name__,

> ex.args) print message

>

> It doesn't raise an exception but it also doesn't save it into the

> field.

 

Because this is set_rfid not save(). If you call save() after calling set_rfid, the value isn't saved?

--

Melvyn Sopacua

ludovic coues

unread,
Feb 23, 2017, 7:07:34 AM2/23/17
to django...@googlegroups.com
Notice how Brian print self.rfid before and after affecting the new value.
The second print should display the updated data. As it is not the
case, I highly doubt save will change the value in db.


Ok, look like I misread the code when writing my previous reply.
Now I'm all curious about how did you find about the editable argument
and why you set it.
About your problem, django's BinaryField is not editable. Here is the
source for django 1.10.5 [1]
Notice how the init method override your argument ? I haven't checked
further but I'm pretty sure
it explain the issue you have noticed.

Doing a raw SQL query to update the RFID value is probably your best
option. Here is the doc [2]

[1] https://github.com/django/django/blob/1.10.5/django/db/models/fields/__init__.py#L2306
[2] https://docs.djangoproject.com/en/1.10/topics/db/sql/#executing-custom-sql-directly
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/10747751.VrTcuDJZbZ%40devstation.

Melvyn Sopacua

unread,
Feb 23, 2017, 9:31:13 AM2/23/17
to django...@googlegroups.com

On Thursday 23 February 2017 13:06:31 ludovic coues wrote:

> Notice how Brian print self.rfid before and after affecting the new

> value. The second print should display the updated data. As it is not

> the case, I highly doubt save will change the value in db.

 

The two prints are different. How did you assert that they're not updated?

 

> Ok, look like I misread the code when writing my previous reply.

> Now I'm all curious about how did you find about the editable argument

> and why you set it.

> About your problem, django's BinaryField is not editable. Here is the

> source for django 1.10.5 [1]

> Notice how the init method override your argument ? I haven't checked

> further but I'm pretty sure

> it explain the issue you have noticed.

 

Editability is a hint for forms, nothing more. Doesn't affect the operation of save, not even Model(not_editable_value='foo').

It merely means, that you will implement saving the value without the help of ModelForm.

 

--

Melvyn Sopacua

Reply all
Reply to author
Forward
0 new messages