I’d like to set custom name to a FileField object in my admin form so that it’s html name attribute will not be equal to my instance field name.
class MyAdminForm(forms.ModelForm):
file_field = forms.FileField()
def __init__(self, *args, **kwargs):
if kwargs.has_key('instance'):
instance = kwargs['instance']
self.initial['image_file'] = instance.file_field
With this code I get <input type=”file” name=”file_field” /> and what I want to do is set it’s name attribute to something else.
EDIT:
I accepted the answer below, but I have another question. Is it possible to construct variable number of FileField objects? I mean – what if I’d like to have 4 of those now, but under some circumstances only one? Will I have to declare all of those as a class fields, like file_field1, file_field2 and so on, or is it possible to add them to a dictionary, like that: { ‘file_field1: FileField(), ‘file_field2’ : FileField() } – I actually tried it and got an error…
The name attribute in the HTML is the same as the name in the form definition so if you don’t want it to be
file_fieldthen don’t call itfile_field.