Re: Access to field name in upload_to?
> One idea would be to put 'fieldname' as the first parameter to the > function, then use functools.partial [1] to create partial functions > for each file field with the value set appropriately: > > thumbnail_image = FileField(upload_to=partial(get_upload_path, > 'thumbnail_image')) Outstanding, thanks. I actually thought to myself "I need a decorator type thing that I can pass a value with but still have it behave like a callable" but didn't know about functools.partial. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Access to field name in upload_to?
On Oct 26, 4:29 pm, Scott Gould wrote: > Hi folks, > > I've got all my file uploads (that go to S3 as it happens, but I don't > think that's overly important) taking their path from one upload_to > delegate: > > def get_upload_path(instance, filename=None): > """ > Defaults to appname/modelname/uuid. > """ > return "%s/%s/%s" % ( > instance.__class__._meta.app_label, > instance.__class__.__name__.lower(), > instance.uuid) > > That's obviously predicated on a field called "uuid" on any model that > uses this method for calculating the upload path. This works fine, but > I now want to deploy the same system on models with multiple > FieldFields. The way I'd like to extend this is by appending the field > name onto the existing file structure: > > def get_upload_path(instance, filename=None): > return "%s/%s/%s/%s" % ( > instance.__class__._meta.app_label, > instance.__class__.__name__.lower(), > instance.uuid, > APPROPRIATE_FIELD_NAME_HERE) > > Which should result in something like "myapp/mymodel// > pdf_file" and "myapp/mymodel//thumbnail_image" given this > model: > > class MyModel(models.Model): > pdf_file = FileField(upload_to=get_upload_path) > thumbnail_image = FileField(upload_to=get_upload_path) > > Any ideas how I can get that field name inside the upload_to function? > Thanks! One idea would be to put 'fieldname' as the first parameter to the function, then use functools.partial [1] to create partial functions for each file field with the value set appropriately: thumbnail_image = FileField(upload_to=partial(get_upload_path, 'thumbnail_image')) [1]: http://docs.python.org/library/functools.html#functools.partial -- DR. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Access to field name in upload_to?
Hi folks, I've got all my file uploads (that go to S3 as it happens, but I don't think that's overly important) taking their path from one upload_to delegate: def get_upload_path(instance, filename=None): """ Defaults to appname/modelname/uuid. """ return "%s/%s/%s" % ( instance.__class__._meta.app_label, instance.__class__.__name__.lower(), instance.uuid) That's obviously predicated on a field called "uuid" on any model that uses this method for calculating the upload path. This works fine, but I now want to deploy the same system on models with multiple FieldFields. The way I'd like to extend this is by appending the field name onto the existing file structure: def get_upload_path(instance, filename=None): return "%s/%s/%s/%s" % ( instance.__class__._meta.app_label, instance.__class__.__name__.lower(), instance.uuid, APPROPRIATE_FIELD_NAME_HERE) Which should result in something like "myapp/mymodel// pdf_file" and "myapp/mymodel//thumbnail_image" given this model: class MyModel(models.Model): pdf_file = FileField(upload_to=get_upload_path) thumbnail_image = FileField(upload_to=get_upload_path) Any ideas how I can get that field name inside the upload_to function? Thanks! -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.