Я пишу какое-то решение:
class CustomClearableFileInput(ClearableFileInput):
def render(self, name, value, attrs=None):
if len(<YourModel>.objects.filter(id=self.form_instance_id))>0:
file = <YourModel>.objects.get(id=self.form_instance_id).<yourField>
else:
file = ''
substitutions = {
'initial_text': self.initial_text,
'input_text': self.input_text,
'clear_template': '',
'clear_checkbox_label': self.clear_checkbox_label,
}
template = '%(input)s'
substitutions['input'] = super(ClearableFileInput, self).render(name, value, attrs)
self.template_with_initial = ('<p class="file-upload">%s</p>'
% self.template_with_initial)
self.template_with_clear = ('<span class="clearable-file-input">%s</span>'
% self.template_with_clear)
if value and hasattr(value, "url"):
template = self.template_with_initial
substitutions['initial'] = format_html(self.url_markup_template,
value.url,
force_text(value))
if not self.is_required:
checkbox_name = self.clear_checkbox_name(name)
checkbox_id = self.clear_checkbox_id(checkbox_name)
substitutions['clear_checkbox_name'] = conditional_escape(checkbox_name)
substitutions['clear_checkbox_id'] = conditional_escape(checkbox_id)
substitutions['clear'] = CheckboxInput().render(checkbox_name, False, attrs={'id': checkbox_id})
substitutions['clear_template'] = self.template_with_clear % substitutions
url = '' if file == '' else file.url
else:
template = self.template_with_initial
substitutions['initial'] = format_html(self.url_markup_template,
url,
force_text(file))
if not self.is_required:
checkbox_name = self.clear_checkbox_name(name)
checkbox_id = self.clear_checkbox_id(checkbox_name)
substitutions['clear_checkbox_name'] = conditional_escape(checkbox_name)
substitutions['clear_checkbox_id'] = conditional_escape(checkbox_id)
if fav == '':
substitutions['clear'] = CheckboxInput().render(checkbox_name, False, attrs={'id': checkbox_id, 'disabled': 'disabled'})
else:
substitutions['clear'] = CheckboxInput().render(checkbox_name, False, attrs={'id': checkbox_id})
substitutions['clear_template'] = self.template_with_clear % substitutions
return mark_safe(template % substitutions)
И тогда в своей форме вы должны написать:
class <YourModel>Form(ModelForm):
class Meta:
model = <YourModel>
fields = '__all__'
widgets= {'<YourField>': CustomClearableFileInput}
def __init__(self, *args, **kwargs):
super(OperatorSettingsForm, self).__init__(*args, **kwargs)
self.fields['<YourField>'].widget.form_instance_id = self.instance.id
Это работает для меня. Я думаю, у вас тоже не будет проблем:)