Search code examples
sql-serverdjangodjango-modelssqlfilestreamdjango-mssql

SQL Server Filestream with django-mssql


I would like to use SQL Server Filestream feature to store files (mainly large images) from a Django application. Currently I am using django-mssql as the database backend for my Django project. I don't think there is any existing model field in that package that is linked to a Filestream column in SQL Server.

What would be the best way to use the filestream feature from Django ? Are there any existing packages ? Or do I need to implement my own custom model field, inherited from Django's built-in fields (FileField or BinaryField) ?

Thanks !


Solution

  • Just in case anyone else is interested in using SQL Server Filestream feature, and ends up here, I started working on custom django Fields to support Filestream : a FileStreamDataField that maps to the varbinary(max) FILESTREAM type and a FileStreamField which is a virtual field that wraps the win32 Streaming API.

    import uuid
    
    from django.db import models
    from sql_filestream import FileStreamDataField, FileStreamField, UUIDField
    
    class DocumentModel(models.Model):
        doc_id = UUIDField(default=uuid.uuid4)
        doc_content = FileStreamDataField(identifier_field='doc_id', null=True, blank=True)
        document = FileStreamField('doc_id', 'doc_content') 
    

    You can find it with examples here : https://github.com/rparent/django-mssql-filestream It works well my use case but is certainly incomplete. Contributions welcome !