callable upload_to problem

23 views
Skip to first unread message

Brot

unread,
Oct 20, 2008, 4:05:54 PM10/20/08
to Django users
Hello,

I am using the latest svn-revision 9236 and tried to use a callable in
the upload_to parameter of the ImageField.

The explanation in the django-docs for the instance-parameter of a
callable 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. If I have the following models.py
---------------------------------------------------------------------------------------------------------
01: from django.db import models
02:
03: def get_storage_path(instance, filename):
04: """ defines the storage path for the photologue app """
05: import os.path
06: print "instance: %s" % (instance)
07: print "desc: %s" % (instance.desc)
08: return os.path.join('uploads', filename)
09:
10: class ImageModel(models.Model):
11: image = models.ImageField('image', upload_to=get_storage_path)
12: desc = models.CharField(max_length=100)
13:
14: def __unicode__(self):
15: return u'%s - %s' % (self.image, self.desc)
---------------------------------------------------------------------------------------------------------
the print statement in line 7 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" 200
2822
[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
---------------------------------------------------------------------------------------------------------

Are there any suggestions?

~Bernd

Brot

unread,
Oct 21, 2008, 7:42:50 PM10/21/08
to Django users
Is there nobody who can help me with my problem? Is this a bug and
should I open a ticket or is it my fault?

~Bernd

Karen Tracey

unread,
Oct 21, 2008, 8:57:28 PM10/21/08
to django...@googlegroups.com
On Tue, Oct 21, 2008 at 7:42 PM, Brot <schl...@gmail.com> wrote:

Is there nobody who can help me with my problem? Is this a bug and
should I open a ticket or is it my fault?


It's either a bug or behavior that needs more explicit documentation.  The problem is that upload_to is called when the field is saved (that is when the form data is copied to the new/changed model instance), and the model is saved field-by-field in (apparently, excepting for id which comes first) the order they are declared in the model.  So, since your ImageField is declared before desc, desc has not yet been set in the model instance when upload_to is called.  If you switch the order of the fields in the model your upload_to will be able to access the desc that has been entered in the form.

I tend to think it's a bug and file-type fields should be saved after non-file fields, since the doc here:

http://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.FileField.upload_to

only mentions the primary id field as one you cannot rely on in upload_to.  But even if I'm wrong about that it deserves a ticket since either the code should be fixed or the doc should mention this behavior.

Karen

Reply all
Reply to author
Forward
0 new messages