Opened 10 years ago

Closed 10 years ago

#23536 closed Bug (invalid)

BinaryField on mysql gets retrieved as 'str', not as 'bytes'

Reported by: fbrau Owned by: nobody
Component: Uncategorized Version: 1.6
Severity: Normal Keywords: mysql BinaryField
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I have a project that uses BinaryFields to pickle dynamic data.

Last week when migrating from sqlite to mysql
using MySQL-for-Python-3 the BinaryField now gets
retrieved as 'str'.

Reading the docs about BinaryField it says it is stored as 'bytes'.

I know mysql stores blobs
as text (right?) but in that case the problem is django's
documentation.

In this particular case:
the attribute mymodel.myblob looked like: b'dadadadaa'
but type(mymodel.myblob) said it was a 'str'
(I'm bit outdated about latest python changes in strings, bytes

and unicode)

Anyways I had to bypass this different behaviour to act just
like the embedded sqlite driver. My 'solution' is not nice:

  • I had to reparse the string to make it a 'bytes':

    self._tags = eval('b"'+self.tags[2:-1]+'"')

  • And add proxy points to get and set the blob

The code is located at:
https://code.google.com/p/tidy-django-models/source/diff?spec=svn2dd52f4b6600c58b9a970cdcebfaf0b725cd0961&r=2dd52f4b6600c58b9a970cdcebfaf0b725cd0961&format=side&path=/tdmodels/tagged.py

Change History (1)

comment:1 by Claude Paroz, 10 years ago

Resolution: invalid
Status: newclosed

This is a known limitation, documented in the third note here: https://docs.djangoproject.com/en/dev/ref/databases/#id8

Now there's hope with a new fork of MySQLdb that we plan to promote (#23446), https://pypi.python.org/pypi/mysqlclient/
If you make positive experiments with that driver, might be nice to comment on that ticket.

Note: See TracTickets for help on using tickets.
Back to Top