views:

286

answers:

2

Hi,

I would like to have 2 ImageFields in a Model.

  1. being the one where the user uploads an image
  2. being one where we have a copy of that image, but using the Same File.

Note: I am simplfying the reason for the two fields.

Apart from Creating a new ImageField field type is there any way to stop the ImageField adding a _ to the filename, when we assign the second field programatically?

Regards

Mark

A: 

Is there a good reason you're not using a filefield for the second entry? It doesn't sound like you're using the specifics of an image field (that is, the admin verifies that it's an image on upload) for that second item. If you need it to be an image field for some reason in your code, you could cast it...

I assume you've got the second field set to hidden in the admin interface?

Paul McMillan
Thanks Paul, I am hiding the second field in the admin interface. The FileField still does the filename collision as far as I know. I need to keep them the same, but field type for an external team using second field in an API.I will look into other options, thanks again for your input!
Mark Ellul
A: 

I know you say "apart from creating a new ImageField type", but I'd imagine that the best way to do this would be to do just that, and override the get_filename method of FileField (which ImageField obviously subclasses).

Apart from that, it is possible to create your own Storage (django.core.files.storage) subclass, which you would pass as the storage argument to the ImageField in your mode. You would need to override the get_valid_name method there. To note is that the actual underscores come from the get_valid_filename method in django.utils.text.

I really don't what's wrong with subclassing ImageField to provide the necessary functionality, though.

obeattie
Thanks for your response! Basically I was looking to see if there was a configuration/parameter change that could help
Mark Ellul