{{{
def user_directory_path(instance, filename):
# file will be uploaded to MEDIA_ROOT/user_<id>/<filename>
return 'user_{0}/{1}'.format(instance.user.id, filename)
class MyModel(models.Model):
upload = models.FileField(upload_to=user_directory_path)
}}}
I'm wondering if this isn't more convenient by defining this as an
instance method, so:
{{{
class MyModel(models.Model):
def user_directory_path(self, filename):
# file will be uploaded to MEDIA_ROOT/user_<id>/<filename>
return 'user_{0}/{1}'.format(self.user.id, filename)
upload = models.FileField(upload_to=user_directory_path)
}}}
Then the function can be used as a method, for example to just determine a
file path without having to use it in the `FileField` per se. It also
avoid defining all "standalone" functions that seem to be coupled to a
model, and thus will make the `models.py` file less chaotic.
--
Ticket URL: <https://code.djangoproject.com/ticket/33944>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* status: new => closed
* resolution: => invalid
Comment:
Hi Willem. I think you're likely free to do that, but I think questions
such as this are better targeted at support channels. See
TicketClosingReasons/UseSupportChannels.
(I don't think it's a change we need to enforce, or favour in the docs
even.)
--
Ticket URL: <https://code.djangoproject.com/ticket/33944#comment:1>