1

I have the following model that includes a file upload by a user.

def resume_path(instance, filename):
    # file will be uploaded to MEDIA_ROOT/user_<id>/resume/<filename>
    return 'user_{0}/resume/{1}'.format(instance.student_user.id, filename)

class Resume(models.Model):
    resume = models.FileField(upload_to=resume_path, blank=True, null=True)
    pub_date = models.DateTimeField(default=timezone.now)
    student_user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)

Then, I would like to allow a user to select one of their uploaded files in a later form. So, I need to be able to dynamically set the path to the directory containing that user's files similar to the dynamic way that I set the upload_path in the original model.

I've tried the following according to this link:

def resume_directory_path(instance):
    # returns the path: MEDIA_ROOT/user_<id>/resume/
    return 'user_{0}/resume/'.format(instance.student_user.id)

class JobApplication(models.Model):
    student_user = models.ForeignKey(StudentUser, on_delete = models.CASCADE)
    resume = models.FilePathField(path=resume_directory_path, null=True)

However, looking at the documentation for FilePathField in Django 3.0, it doesn't look like it takes a callable for the path attribute. So, I'm not sure how the answer in the above link answers my question. What is the best way to achieve this functionality?

I'd like to do something like the below:

class CallableFilePathField(models.FilePathField):

    def __init__(self, *args, **kwargs):
        kwargs['path'] = resume_directory_path(instance)
        super().__init__(*args, **kwargs)

class JobApplication(models.Model):
    student_user = models.ForeignKey(StudentUser, on_delete = models.CASCADE)
    resume = models.CallableFilePathField(path=resume_directory_path, null=True)

The trouble is that I don't know how to properly reference the model instance in this code (so instance is undefined). I've looked at the FileField code to try to see how they do it there, but I couldn't make sense of it.

cmanbst
  • 179
  • 3
  • 13
  • According to the [docs](https://docs.djangoproject.com/en/dev/ref/models/fields/#filepathfield) and the linked answer, you should try: `resume = models.FilePathField(path=resume_directory_path, null=True)` – raratiru Jul 05 '18 at 18:01
  • Then it throws an error that path should be a string, bytes, or os.PathLike or None. I've edited it to reflect (this is actually what I had in my code, so thanks for catching). – cmanbst Jul 05 '18 at 18:14
  • I think it's better not to use FilePathField. It is designed to search a pre-set directory so you don't have to run DB query. In your case, I think it's better just to set your `resume` field as a ForeignKey to your `Resume` class. In your template you can use the reversed query to select resumes. – Mia Jul 05 '18 at 22:35
  • There is currently an Issue and PR to bring this functionality to 3.0 https://code.djangoproject.com/ticket/29529 – Ullullu Mar 22 '19 at 14:15

0 Answers0