Open ygbourhis opened 4 years ago
For your information, the model was generated on a legacy db with sqlacodegen: https://pypi.org/project/sqlacodegen/ I only have read access to the DB and can not modify it.
Blob fields are not auto-mapped to file upload fields as they're not implemented yet. Though, you can try registering a file upload field or a custom serializer field that handles blobs by adding to rest_witchcraft.field_mapping.SERIALIZER_FIELD_MAPPING
.
Thanks @shosca for your reply.
file fields are different in django since they do not store the file data in the db, only the file access and flat files are kept for storage. The django equivalent of sqlalchemy.dialects.mysql.LONGBLOB fields is rather django.db.models.fields.BinaryField:
https://docs.djangoproject.com/en/3.0/ref/models/fields/#binaryfield
MySQL LONGBLOB type is for binary data up to 2^32 bytes.
They are used to store large data in the db.
Exactly, my plan was to do a simple Base64BinaryField
that would dump/read base64 data from the blob columns but never got around to it
One thing that you can do for the api is to defer the content value, so something like this:
class SiteContentSerializer(ModelSerizlier):
class Meta:
model = SiteContent
...
fields = ["key", "mime_type", "created_date", "updated_date"]
class SiteContentViewSet(ModelViewSet):
...
serializer_class = SiteContentSerializer
...
@action(methods=["get"], detail=True)
def value(self):
... return the blob here ....
This way you will have content/<pk>/value
giving you the blob
@shosca for the moment I just replaced LONGBLOB by LONGTEXT(binary=True) and it seems to work. With LONGTEXT(binary=True) the ORM returns bytes instead of text.
In fact this field can hold anything: text, png, etc... and with LONGTEXT(binary=True) I retrieve for PNG images b'\x89PNG\r\n\x1a\n\x00\x00\...' (etc...)
If I then do an open('my_file', 'wb')
and write the content, I get what I expect (a png if it was a png, etc...).
I have a class like this one:
the issue is with sqlalchemy.dialects.mysql.LONGBLOB fields I get this kind of tracebacks:
there seems to be an issue with LONGBLOB fields.