I’m trying to save files in a specific folder depending on a foreign key of the model. This is the model where files are uploaded:
class PersonVisual(models.Model):
person = models.ForeignKey(Person, on_delete=models.CASCADE, related_name='visual')
file = models.FileField(upload_to=upload_to_people_instance_folder)
class Meta:
verbose_name_plural = "people visuals"
This is the function for upload_to:
def upload_to_people_instance_folder(instance, filename):
clean_name = f"{instance.person.first_name}_{instance.person.last_name}"
return f"/people/visual/{clean_name}/{filename}"
That did what was expected, but I get the following error:
SuspiciousFileOperation at /admin/others/person/1/change/
Detected path traversal attempt in '/people/visual/firstname_lastname/filename.jpg'
So thats clearly not the way to go, maybe Im missing something?