我假設的模型是這樣的:現在
class Patient(models.Model):
name = models.CharField(max_length=100)
.......
.......
class Appointment(models.Model):
patient = models.ForeignKey(Patient, on_delete=models.CASCADE)
.......
.......
class AppointmentFiles(models.Model):
appointment = models.ForeignKey(Appointment, on_delete=models.CASCADE)
........
........
path = 'uploads/'+ appointment__patient__name + '/' + appointment__id + '/'
file = models.FileField(upload_to=path)
,它都將文件存儲到上傳/ patient_name/appointment_number/
其中patient_name和appointment_number會爲每一位患者而異。
每個文件將被保存爲
上傳/ patient_name/appointment_number/filename.extension
你試過沒它爲你工作? –