0
class Project(models.Model):
    slug = models.SlugField(max_length=100)
    main_file = models.FilePathField(path="/home/mn/myfiles/%s" % slug)

This code doesn't work, just illustrating what I'd like to do. I want to fill in FilePathField when object is accessed (ie. when it is updated in django admin, not when it's created).

Is there a hook where you can set fields just before they are filled? Something like object.on_get(instance): ... perhaps?

1 Answer 1

1

It's unclear what your intentions here are.

Since Model instances are just Python classes, you can do this by a method on the Project class:

class Project(models.Model):
    slug = models.SlugField(max_length=100)

    @property
    def main_file(self):
         return "/home/mn/myfiles/%s" % self.slug

If you want to manually specify a location outside of MEDIA_ROOT to upload files to, you will need to subclass django.db.models.fields.files.FileField and overwrite the __init__ method.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.