Using django-filer, can I chose the folder that images go into, from Unsorted Uploads

2024/10/8 8:32:43

I'm using django-filer for the first time, and it looks great, and work pretty well.

But all my images are being uploaded to the 'Unsorted Uploads' folder, and I can't figure out a way to put them in a specific one. This strikes me as a fundamental feature, and given that it allows you create folders, this must be possible, right? But I can't find it in the docs, and a quick look through the source didn't help me.

I have a basic setup like:

class Story(models.Model):image = FilerImageField()class Gift(models.Model):# some stuffclass GiftImage(models.Model):gift = models.ForeignKey(Gift)image = FilerImageField()

And I want GiftImage.image and Story.image to go into separate folders, so as to make sorting/searching easier for the admin user.

I have tried

image = FilerImageField(upload_to='somewhere') # How a django ImageField would do it
image = FilerImageField(folder='somewhere') # a guess
image = FilerImageField(name='somewhere') # a guess
image = FilerImageField(folder_name='somewhere') # another guess

All of these either give me a "TypeError: init() got an unexpected keyword argument ..." or just don't do what I was hoping.

Cheers!

Answer

In case anyone else comes across this, here is what I found for a workaround. If you give the field a default value, then the upload will default to whatever folder that default image is in. In my particular case, I wanted a default image anyways so this killed two birds with one stone.

Here is how you can do that:

First, note that FilerImageField is really a foreign key to filer.Image. So, to add a default, you need to first add a filer.Image to your database to use as the default, putting it in the folder you want uploads to go to. How you do that is up to you -- via your admin, or a data migration somehow, perhaps. Then you can make that image the default using a method, like this:

def get_default_image():from filer.models.imagemodels import Imagetry:return Image.objects.filter(name='My Default Image').order_by('-uploaded_at')[0]except IndexError:return Noneclass MyModel(models.Model):image = FilerImageField(null=True, blank=True, default=get_default_image)

This implementation has some obvious caveats. If your default image name changes you are going to be back to having no default image (and, thus, uploading to 'Unsorted Uploads'). And if there are multiple with that name, your method has to choose one. I choose the most recently uploaded one.

If you are confident you'll never want to change the default image instance you could just select it by id.

So this is a bit kludgy but if you are scrambling to get something working maybe you are ok with that. Hope this helps someone.

https://en.xdnf.cn/q/70136.html

Related Q&A

how to use distutils to create executable .zip file?

Python 2.6 and beyond has the ability to directly execute a .zip file if the zip file contains a __main__.py file at the top of the zip archive. Im wanting to leverage this feature to provide preview r…

Pass nested dictionary location as parameter in Python

If I have a nested dictionary I can get a key by indexing like so: >>> d = {a:{b:c}} >>> d[a][b] cAm I able to pass that indexing as a function parameter? def get_nested_value(d, pat…

Correct way to deprecate parameter alias in click

I want to deprecate a parameter alias in click (say, switch from underscores to dashes). For a while, I want both formulations to be valid, but throw a FutureWarning when the parameter is invoked with …

How to rename a file with non-ASCII character encoding to ASCII

I have the file name, "abc枚.xlsx", containing some kind of non-ASCII character encoding and Id like to remove all non-ASCII characters to rename it to "abc.xlsx".Here is what Ive t…

draw random element in numpy

I have an array of element probabilities, lets say [0.1, 0.2, 0.5, 0.2]. The array sums up to 1.0.Using plain Python or numpy, I want to draw elements proportional to their probability: the first eleme…

Python Gevent Pywsgi server with ssl

Im trying to use gevent.pywsgi.WSGIServer to wrap a Flask app. Everything works fine, however, when I try to add a key and a certificate for ssl, its not even able to accept any clients anymore.This is…

unexpected keyword argument buffering - python client

I am receiving the error as "getresponse() got an unexpected keyword argument buffering". Complete error log is :[INFO ] Kivy v1.8.0 [INFO ] [Logger ] Record lo…

numpy and pandas timedelta error

In Python I have an array of dates generated (or read from a CSV-file) using pandas, and I want to add one year to each date. I can get it working using pandas but not using numpy. What am I doing wron…

Pandas - split large excel file

I have an excel file with about 500,000 rows and I want to split it to several excel file, each with 50,000 rows.I want to do it with pandas so it will be the quickest and easiest.any ideas how to make…

Unable to verify secret hash for client at REFRESH_TOKEN_AUTH

Problem"Unable to verify secret hash for client ..." at REFRESH_TOKEN_AUTH auth flow. {"Error": {"Code": "NotAuthorizedException","Message": "Unab…