#13784 closed (duplicate)
pre_save is dispatched before executing upload_to attributes on FileFields when using ModelForm
Reported by: | Peter Bengtsson | Owned by: | nobody |
---|---|---|---|
Component: | Forms | Version: | 1.2 |
Severity: | Keywords: | ModelForm pre_save | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Suppose you have this code:
def callable_upload_to(instance, filename): full_path = os.path.join(this_year(), filename) print "PLACING", full_path return full_path class Foo(models.Model): file = models.FileField(upload_to=callable_upload_to) size = models.IntegerField(null=True) def update_foo(sender, instance, **kwargs): print "UPDATING", str(instance.file) pre_save.connect(update_foo, sender=Foo)
Suppose you create one of these instances with a ModelForm you'll get this output on stdout:
UPDATING sample.file PLACING 2010/sample.file
That means that you can't rely on the file path as dictated by the upload_to callable in your pre_save signal. Especially important is that within your pre_save
method you can't even get to the file since it doesn't exist.
Change History (3)
comment:1 by , 14 years ago
comment:2 by , 14 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
This behavior got a fair amount of discussion when it was introduced. See #10788 and the linked threads on django-developers.
comment:3 by , 14 years ago
Oh I see. I really did google the tickets but didn't find that one.
Personally thinks it sucks but will keep my mouth shut till I provide a patch of my own :)
I suspect that the culprit is
Model.save_base
indjango/db/models/base.py
where it dispatches the signal before it does whatever it does to thosemeta.local_files
but I don't understand the code enough to attempt a patch at this point.