So I have a Model that will contain a couple hundred instances and I now added an imagefield logo. As I don't want to upload each and every logo via the admin manually, I want to set up a callable that returns the correct file path so I can just push the logos to media/company_logos. Due to some technical hick-up I can push the logos to the folder only after creating their instance.
# models.py
def logo_directory_path(instance, filename):
return f'company_logos/{instance.symbol}'
class Company(models.Model):
symbol = models.CharField(max_length=50)
logo = models.ImageField(upload_to='company_logos/', default=logo_directory_path(instance=symbol, filename=f'{symbol}.png'))
This returns an error at instance=symbol saying
Type 'str' doesn't have expected attribute 'symbol'
Another approach was
logo = models.ImageField(upload_to='company_logos/', default=f'company_logos/{symbol}')
which returns a URL as src="/media/company_logos/<django.db.models.fields.CharField>"
I am not quiet sure how to set this up as I cant fully understand the official docs.
My desired outcome is to have a URL for default like so:
company_logos/IBM.png with field symbol being the corresponding part (I tried it via f-string).
Update to Ankits request --> When trying to create a new instance with the code suggested it throws
logo_directory_path() missing 2 required positional arguments: 'instance' and 'filename'