upload_to doesn't work like excepted
|Reported by:||Bernd Schlapsi||Owned by:||nobody|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
I am using the latest svn-revision 9244 and tried to use a callable in the upload_to parameter of my ImageField.
The explanation in the django-docs for the instance-parameter of acallable says the following:
An instance of the model where the FileField is defined. More specifically, this is the particular instance where the current file is being attached.
If I understand this right, I have access to all the input data of this instance (excepting the id). If I have the following models.py
01: from django.db import models 02: 03: def get_storage_path(instance, filename): 04: import os.path 05: print "instance: %s" % (instance) 06: print "desc: %s" % (instance.desc) 07: return os.path.join('uploads', filename) 08: 09: class ImageModel(models.Model): 10: image = models.ImageField('image', upload_to=get_storage_path) 11: desc = models.CharField(max_length=100) 12: 13: def __unicode__(self): 14: return u'%s - %s' % (self.image, self.desc)
the print statement in line 6 should print all the data I inserted into this variable in the admin. Is that right? In my case this is always a empty string:
[20/Oct/2008 21:52:47] "GET /admin/hp/imagemodel/add/ HTTP/1.1" 2002822 [20/Oct/2008 21:52:47] "GET /admin/jsi18n/ HTTP/1.1" 200 1915 instance: - desc: [20/Oct/2008 21:52:54] "POST /admin/hp/imagemodel/add/ HTTP/1.1" 302 0 [20/Oct/2008 21:52:54] "GET /admin/hp/imagemodel/ HTTP/1.1" 200 1866
But this is only true if I change the order of the two fields in my model. It seems that the order in the model is important for this behavior. But in my production case I subclass a model of an reusable app (photologue) and so I have no impact for the right order of my fields!
I tend to think it's a bug and file-type fields should be saved after non-file fields.
Change History (11)
comment:1 Changed 8 years ago by Bernd Schlapsi
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset