#33944: Is it cleaner to make the upload_to function a method of the model class
--------------------------------------------+------------------------
Reporter: Willem Van Onsem | Owner: nobody
Type: New feature | Status: new
Component: Documentation | Version: dev
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
--------------------------------------------+------------------------
In the documentation of the `upload_to` field
(https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.FileField.upload_to),
it works with an example:
{{{
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.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/01070182bd417f11-1621223b-633c-4d4a-872d-d7436f1f5f75-000000%40eu-central-1.amazonses.com.