On 2/9/08, bergstyle <[EMAIL PROTECTED]> wrote:
>
> I know there's been lots of discussion about how to change uploaded
> filenames. After wading through the options I've been  trying to use
> the method described here: 
> http://gulopine.gamemusic.org/2007/nov/07/customizing-filenames-without-patching/

I added a note on the sidebar for that article, but maybe I should
make it even more clear, so this doesn't keep happening. Please don't
use that article, it's really not a very good way to go about it. I
wrote it thinking it was the *only* way, and that something was better
than nothing. But there are better ways.[1]

> The result with this is "None_None_originalfilename.ext". Is it not
> possible to get the pk at this point in object's lifecycle? Has the pk
> even been generated yet? I'm new to Django so I'm not sure what has
> and hasn't executed at the point where this method is running. I
> imagine save hasn't because it's called right after I'm trying to
> rename the file, but why would the author suggest retrieving the pk
> value before it's made available? Am I missing something? Does Django
> know what the pk value will be ahead of time?

No, Django doesn't know the primary key value before the object is
saved, because it's generated by the database (in 99% of cases,
anyway). In order to do what you're looking at, you'll need to hit the
database twice, unfortunately. Once to save the object without the
file, to populate the primary key, and again to save the filename that
was created with the new primary key.

> Any help is appreciated. I've also tried this technique
> http://code.djangoproject.com/wiki/CustomUploadAndFilters and  the pk
> shows up as None using that code as well. I'm using 0.96.

Well, as stated, your problem is that you're trying to use the primary
key of the actual object you're trying to save. You'll notice that in
my article, the primary key I used was of a related object, which
would already exist before trying to save the new object. The problem
you're having will happen regardless of what method you use, even
after #5361 goes in. There's just no avoiding it.

The closest you can get would be to do something like this, if you
insist on using my method.

def _save_FIELD_file(self, file, filename, raw_contents,
save=True):
    self.save()
    filename = "%s_%s_%s" % (self.id, self._get_pk_val(),
filename)
    super(Product, self)._save_FIELD_file(file, filename,
raw_contents, save)

But personally, I'd recommend you find a naming scheme that doesn't
rely on the primary key of the Product object. Perhaps set up a
separate Image model, which can then reference the Product by a
primary key?

-Gul

[1] 
http://scottbarnham.com/blog/2007/07/31/uploading-images-to-a-dynamic-path-with-django/

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to