I am implementing multitenant in a django project with PostgreSQL for this I implemented a library called django-tenants but I have a small problem what I wanted was to save the media files for each scheme in a folder with the same name of the scheme for it in the official documentation It told me that I had to put this configuration in my settings.py.
DEFAULT_FILE_STORAGE = 'django_tenants.files.storage.TenantFileSystemStorage
MULTITENANT_RELATIVE_MEDIA_ROOT = ""
To be able to save a folder of the media of my models for each scheme, when I test my model
class Product(models.Model):
name = models.CharField(max_length=150, verbose_name='Name')
image = models.ImageField(upload_to='product/%Y/%m/%d', null=True, blank=True, verbose_name='Image')
Actually saving in the correct folder is called schema/product/2023/08/30/imagen.png but when I consult the path in the database it only saves me product/2023/08/30/imagen.png how do I get it to be save the schematic also in the path?
I made this code to solve the problem
def image_upload_path(base_path):
def upload_path(instance, filename):
tenant = connection.get_tenant()
current_date = datetime.now()
return f"{tenant.schema_name}/{base_path}/{current_date.year}/{current_date.month}/{current_date.day}/{filename}"
return upload_path
image = models.ImageField(upload_to=image_upload_path('product'), null=True, blank=True, verbose_name='Image')
It works but I would like to make it more general to avoid repeating code that affects all fields that are image or file