Django表单:在提交数据库之前要求确认

tBu*_*uLi 5 django django-forms

更新:该解决方案可以作为单独的答案找到

我正在制作Django表单,以允许用户向我的数据库添加电视节目。为此,我有一个Tvshow模型,TvshowModelForm并且使用通用的基于类的视图CreateTvshowView/ UpdateTvshowView生成表单。

现在出现了我的问题:假设用户想向数据库添加节目,例如《权力的游戏》。如果已经存在以该标题显示的节目,我想提示用户确认这确实是与数据库中的节目不同的节目,如果不存在类似的节目,我想将其提交给数据库。我如何最好地处理此确认?

我的一些实验显示在下面的代码中,但也许我正在以错误的方式进行操作。我的解决方案的基础是包括一个隐藏字段force,如果用户确定是否要提交此数据,则如果系统提示他,则应将其设置为1,以便我可以读取该值是否为1来确定用户是否单击再次提交,从而告诉我他要存储它。

我很想听听你们对如何解决这个问题的想法。

views.py

class TvshowModelForm(forms.ModelForm):
    force = forms.CharField(required=False, initial=0)
    def __init__(self, *args, **kwargs):
        super(TvshowModelForm, self).__init__(*args, **kwargs)

    class Meta:
        model = Tvshow
        exclude = ('user')

class UpdateTvshowView(UpdateView):
    form_class = TvshowModelForm
    model = Tvshow
    template_name = "tvshow_form.html"

    #Only the user who added it should be allowed to edit
    def form_valid(self, form):
        self.object = form.save(commit=False)
        #Check for duplicates and similar results, raise an error/warning if one is found     
        dup_list = get_object_duplicates(Tvshow, title = self.object.title)
        if dup_list:
            messages.add_message(self.request, messages.WARNING, 
'A tv show with this name already exists. Are you sure this is not the same one? Click submit again once you\'re sure this is new content'
               )
#            Experiment 1, I don't know why this doesn't work
#            form.fields['force'] = forms.CharField(required=False, initial=1)

#            Experiment 2, does not work: cleaned_data is not used to generate the new form
#            if form.is_valid():
#                form.cleaned_data['force'] = 1

#            Experiment 3, does not work: querydict is immutable
#            form.data['force'] = u'1'

        if self.object.user != self.request.user:
            messages.add_message(self.request, messages.ERROR, 'Only the user who added this content is allowed to edit it.')

        if not messages.get_messages(self.request):
            return super(UpdateTvshowView, self).form_valid(form)
        else:
            return super(UpdateTvshowView, self).form_invalid(form)
Run Code Online (Sandbox Code Playgroud)

tBu*_*uLi 6

解决方案

借助此处发布的答案解决了这个问题,特别是 Alexander Larikov 和 Chris Lawlor 的答案,我想发布我的最终解决方案,以便其他人可以从中受益。

事实证明,用 CBV 可以做到这一点,我很喜欢它。(因为我喜欢保持一切 OOP)我还使表单尽可能通用。

首先,我制作了以下表格:

class BaseConfirmModelForm(BaseModelForm):
    force = forms.BooleanField(required=False, initial=0)

    def clean_force(self):
        data = self.cleaned_data['force']
        if data:
            return data
        else:
            raise forms.ValidationError('Please confirm that this {} is unique.'.format(ContentType.objects.get_for_model(self.Meta.model)))

class TvshowModelForm(BaseModelForm):            
    class Meta(BaseModelForm.Meta):
        model = Tvshow
        exclude = ('user')

"""
To ask for user confirmation in case of duplicate title
"""
class ConfirmTvshowModelForm(TvshowModelForm, BaseConfirmModelForm):
    pass   
Run Code Online (Sandbox Code Playgroud)

现在正在制作合适的视图。这里的关键是发现 get_form_class 而不是使用 form_class 变量。

class EditTvshowView(FormView):       
    def dispatch(self, request, *args, **kwargs):
        try:
            dup_list = get_object_duplicates(self.model, title = request.POST['title'])  
            if dup_list:         
                self.duplicate = True
                messages.add_message(request, messages.ERROR, 'Please confirm that this show is unique.')
            else:
                self.duplicate = False
        except KeyError:
            self.duplicate = False
        return super(EditTvshowView, self).dispatch(request, *args, **kwargs)

    def get_form_class(self):
        return ConfirmTvshowModelForm if self.duplicate else TvshowModelForm

"""
Classes to create and update tvshow objects.
"""
class CreateTvshowView(CreateView, EditTvshowView):  
    pass

class UpdateTvshowView(EditTvshowView, UpdateObjectView):
    model = Tvshow  
Run Code Online (Sandbox Code Playgroud)

我希望这能让其他有类似问题的人受益。


Ale*_*kov 3

我会将其作为答案发布。在表单的clean方法中,您可以按照您想要的方式验证用户的数据。它可能看起来像这样:

def clean(self):
    # check if 'force' checkbox is not set on the form
    if not self.cleaned_data.get('force'):
        dup_list = get_object_duplicates(Tvshow, title = self.object.title)
        if dup_list:
            raise forms.ValidationError("A tv show with this name already exists. "
                                        "Are you sure this is not the same one? "
                                        "Click submit again once you're sure this "
                                        "is new content")
Run Code Online (Sandbox Code Playgroud)