How to clean a certain field in a InlineFormSet?

*爱你&永不变心* 提交于 2019-12-03 13:45:19

You can set the inline formset to use a form class, and then you can create a clean function for the field.

In myapp/forms.py:

class InlineFormsetForm(forms.Form)
    myfield = forms.CharField(required=False, max_length=50)

    def clean_myfield(self):
        data = self.cleaned_data['myfield']
        if data == 'badinput':
            raise forms.ValidationError("I hates it!")
        return data

Then, in myapp/views.py

from myapp.forms import InlineFormsetForm
from myapp.models import ParentRecord, ChildRecord

def editmything(request):
    MyFormSet = inlineformset_factory(ParentRecord, ChildRecord, form=InlineFormsetForm)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!