django store image in database - django

Does anyone know if there's an out-of-the box way of storing images directly in the database vs. using ImageField model type that simply uploads it to the MEDIA_ROOT.
And if there is, how does one serve those images then?
Cheers

No, there isn't. And for good reason. It's horribly inefficient to store and serve images from the database. Store them on the filesystem, and serve them directly from Apache.

There is a nice solution here: http://djangosnippets.org/snippets/1305/ it stores content in a database blob.

It seems there is no built-in BlobField in Django. However, there is one available here. I'm not sure if it supports all backends, but it might work for you. With that, you could write up a form & view that uploades the image as an attachment and stores it as a blob in the database.

Related

Django ImageField that generates multiple renditions

In the admin of my Django website I let admins upload images at a very high resolution. I want to automatically generate and store several renditions of each uploaded image at specific sizes, and then use the different sized renditions in different places on the website.
What is a good way to do this?
I ended up using django-imagekit.
You can use the image directly in your template and use sorl to generate the thumbnail or image at different size.
https://djangosnippets.org/snippets/1172/
You can also override the model save method, check this small script:
https://djangosnippets.org/snippets/1172/

Is it possible to store one specific Django model/Postgresql table on another sever?

Django natively stores all the content associated with it's models to one server. What if you wanted to store one specific Django model on another server? Is there something you can change in the settings.py to make this happen?
The problem I'm having is that for django apps like [django-messages]: http://files.arnebrodowski.de/software/django-messages/Documentation, the tables can get really large. When the app is installed, it natively creates a table on the original server, however, I'd like for it to have it's own server else where. What are my options?
The answer is in the FineManual(tm), with a quite readable link (labelled "multiple databases") in the upper half of the doc's front page.
https://docs.djangoproject.com/en/1.5/topics/db/multi-db/

Mixing django-filepicker and mongodb

I'm developing a django 1.4 application, and I'm using django-filepicker to upload images. They use a special modelField for images called FPFileField.
I'm interested in storing those images in a MongoDB database, instead of uploading it to a "media" folder or something similar.
Any idea how this can be achieved?
I've tried mongoengine with no luck.
I would recommend storing the url into your mongo Document directly and then making use of the filepicker.io URL and built in S3 support rather than storing the file on your servers directly.
You should look into GridFS
I'd also consider uploading it to amazon's S3 service too - depending on what your requirements are.

Need help setting up django-filetransfers

My setup is: Django 1.3/Python 2.7.2/Win Server 2008 R2/IIS 7.5/MS SQL Server 2008 R2. I am developing an application whose main function is to analyze uploaded files and produce a report.
Reading over the documentation for django-filetransfers, I believe this is a solution to a problem I've been trying to solve for a while (i.e. form-based file uploads completely block all Django responses until the file-transfer finishes...horror for even moderate-sized files).
The documentation talks about piping uploads to S3 or Blobstore, and that might be what I end up doing eventually, but during development I thought maybe I could just set up my own "poor-man's S3" on a server that I control. This would basically just be another Django instance (or possibly a simple ASP.NET app) whose sole purpose is to receive uploaded files. This sounds like it should be possible with django-filetransfers and would solve the problem of Django responsiveness (???).
But I am missing some bits of understanding how this works in general, as well as some specifics. Maybe an example will help: let's say I have MyMainDjangoServer and MyFileUploadServer. MyMainDjangoServer will serve the views, including the upload form. MyFileUploadServer will "catch" the uploaded files. My questions/confusion are as follows:
My upload form will contain additional fields beyond just the file(s)...do I understand correctly that MyMainDjangoServer will somehow still get that form data, minus the file data (basically: request.POST), and the file data gets shunted over to MyFileUploadServer? How does this work? Will MyMainDjangoServer still block during the upload to MyFileUploadServer?
I assume that what I would need to do on MyFileUploadServer is have a view/URL that handles the form request and sucks out the request.FILES data. What else needs to happen? What happens to the rest of the form data?
How would I set up my settings.py for this scenario? The django-filetransfers examples seem to assume either S3 or GAE/Blobstore but maybe I am missing some basics.
Any advice/answers appreciated...this is a confusing and frustrating area of Django for me.
"MyMainDjangoServer will somehow still get that form data, minus the file data (basically: request.POST), and the file data gets shunted over to MyFileUploadServer? How does this work? Will MyMainDjangoServer still block during the upload to MyFileUploadServer?"
I know the GAE Blobstore, presumably S3 as well, handles this by requiring you to give it a success_url. In your case that would be the url on MyMainDjangoServer where your file receiving view on MyFileUploadServer would re-post the non-files form data to once the upload is complete.
Have a look at the create_upload_url method here: https://developers.google.com/appengine/docs/python/blobstore/functions
You need to recreate this functionality in some form (see below).
"How would I set up my settings.py for this scenario?"
You'd need to create your own filetransfers backend which would be a file with a prepare_upload function in it.
You can see the App Engine one here:
https://github.com/django-nonrel/djangoappengine/blob/develop/storage.py
The prepare_upload method just wraps the GAE create_upload_url method mentioned above.
So in your settings.py you'd have something like:
PREPARE_UPLOAD_BACKEND = 'myapp.filetransfers_backend.prepare_upload'
(i.e. the import path to your prepare_upload function)
For the rest you can start with the ones provided by filetransfers already:
SERVE_FILE_BACKEND = 'filetransfers.backends.url.serve_file'
# if you need it:
PUBLIC_DOWNLOAD_URL_BACKEND = 'filetransfers.backends.url.public_download_url'
These rely on the file_field.url being set (see Django docs) and since your files will be on a separate server you probably need to look into writing a custom storage backend for Django too. (the S3 and GAE cases assume you're using the custom Django storage backends from here)

How to change django FileFiled 'storage' AFTER uploading?

I am developing two web sites using the Django framework.
The thing is - one site is sharing part of the content from the other one.
They both use different amazon WS buckets to store images, etc.
So for the site which shares some content with another one I need to specify a different MEDIA_URL, but it seems impossible cause 'upload_to' and 'storage' parameters of the FileField only influence the file being uploaded.
Is there any way to use another storage when displaying image after it was uploaded?