I am trying to set upload paths in Django ImageField with the upload_to attribute with similar to below.
Model
image = models.ImageField(upload_to=image_upload_location(image="logo"))
Function
def image_upload_location(filename, instance, *args, **kwargs):
if image:
defaultFolder = "images/default"
logoFolder = "images/logo"
generalFolder = "images/general"
productsFolder = "images/products"
if image == "logo":
folder = logoFolder
elif image == "general":
folder = generalFolder
elif image == "products":
folder = "productsFolder"
else:
folder = defaultFolder
return "%s/%s" % (folder, filename)
I get the following error:
TypeError: image_upload_location() missing 2 required positional arguments: 'filename' and 'instance'
I've tried to pass instance and filename but can't work out how to make this work. Any ideas on how to make this work so that I can use the same function for ImageField as I'm trying to follow the DRY principal by making a function to handle all the "set" locations.
I also don't want the date fields that Django adds.
[edit] The main thing I need to know here is how to pass the required variable "instance and filename" plus an additional variable "image" to the function from the model ImageField. :-)