D'oh. This was trivial.
>Is there a way to
> save the rest of the object before calling "file_path"?
Yes, putting it after the other fields in the model definition.
This works:
class File(models.Model):
article= models.ForeignKey(Article)
type= models.IntegerField(choices=choices((0,"Text"),(1,"Figure"),
(2,"Table")))
number= models.IntegerField()
file= models.FileField(upload_to=file_path)
On Sep 24, 11:51 am, Rodrigo Culagovski <[EMAIL PROTECTED]> wrote:
> I'm trying to use the new ability to pass a callable to "upload_to" in
> fieldfields. I'm having problems with the object not being saved
> before the callable is called.
>
> Model:
>
> def file_path(instance, filename):
> return "uploads/files/%s/%s/%s_%s%s" % (
> slugify(instance.article.author.lastname
> +"_"+instance.article.author.firstname),
> slugify(instance.article.title),
> instance.get_type_display(),
> instance.number,
> os.path.splitext(filename)[1])
>
> class File(models.Model):
> article= models.ForeignKey(Article)
> file= models.FileField(upload_to=file_path)
> type= models.IntegerField(choices=choices((0,"Text"),(1,"Figure"),
> (2,"Table")))
> number= models.IntegerField()
>
> File is being edited inline in "Article". This should create a path
> like
>
> "uploads/files/peter_parker/webs_i_have_known/Figure_1.jpg",
>
> however, what I actually get is:
>
> "uploads/files/parker_peter/webs_i_have_known/None_None.jpg".
>
> So "instance.get_type_display()" and "instance.number" are returning
> "None".
> If I change the file and save it again the name is assigned
> correctly.
> This is clearly because the object has not been saved the first time
> when the filepath is calculated. How to fix this? Is there a way to
> save the rest of the object before calling "file_path"?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---