I am currently using Cloudinary with Django to store user profile pictures and would like to pass parameters to it store it in a folder and and overwrite the existing image instead of creating a new one.
In my user model:
picture = CloudinaryField('image')
This works as expected with Django Admin and forms. I would just like the ability to store it in the folder users/USERNAME/profile and when someone updates their picture to delete the old one.
update
The solution that worked in 2016 is no longer viable. According to the docs, the following will work:
image = CloudinaryField(
"Image",
overwrite=True,
resource_type="image",
transformation={"quality": "auto:eco"},
format="jpg",
)
The allowed params are listed here and here in the code for version 1.17.0
or here in the docs.
For example, I was confused with the keyword quality
. I was using it directly when using the API but in CloudinaryField
is not allowed.
The proper way to define the quality of the uploaded photo is by setting:
transformation={"quality": 80}
.
This is clarified in the docs where it is explained that:
Note that when using the SDK for a dynamically typed language, the transformation parameters can be specified directly without using this transformation parameter.
This worked perfectly:
from cloudinary.models import CloudinaryField as BaseCloudinaryField
from django.db import models
class CloudinaryField(BaseCloudinaryField):
def upload_options(self, model_instance):
return {
'public_id': model_instance.name,
'unique_filename': False,
'overwrite': True,
'resource_type': 'image',
'tags': ['map', 'market-map'],
'invalidate': True,
'quality': 'auto:eco',
}
class MarketMap(models.Model):
name = models.CharField(max_length=17)
image = CloudinaryField()