开发者

Override FileField widget in InlineFormset_factory?

开发者 https://www.devze.com 2023-01-17 11:34 出处:网络
I would like to c开发者_开发百科hange the default upload field (FileField) in an inlineformset_factory form, to use the AdminFileWidget from django.contrib.admin.widgets.The purpose of this is to show

I would like to c开发者_开发百科hange the default upload field (FileField) in an inlineformset_factory form, to use the AdminFileWidget from django.contrib.admin.widgets. The purpose of this is to show the path of the currently uploaded file as it does in the admin forms (perhaps there is another way to do this anyway?).

I have no trouble getting the widget to work using a custom form, just cant figure out how to change the widget in an inlineformset_factory.


This will get you the Admin FileField widget instead of the standard one with 5 extra fields.

views.py

MySpecialFormset = inlineformset_factory(  MyParentModel, 
                                           MyChildModel, 
                                           form=MyChildModelForm, 
                                           extra=5)

formset = MySpecialFormset(instance=myparentmodelinstance) #add request.POST and request.FILES if used on the POST cycle

forms.py

from django.contrib.admin.widgets import AdminFileWidget

class MyChildModelForm(forms.ModelForm):

    class Meta:
        model = MyChildModel

    def __init__(self, *args, **kwargs):
        super(MyChildModelForm, self).__init__(*args, **kwargs)

        self.fields['my_file_field'].widget = AdminFileWidget() 
0

精彩评论

暂无评论...
验证码 换一张
取 消