简体   繁体   English

Django视图撤销了我的代码已实现的目标

[英]Django view undoing what my code has achieved

My model: 我的模特:

My 'event' references several 'items' through the following declaration: 我的“事件”通过以下声明引用了多个“项目”:

items = models.ManyToManyField('Item')

My Form: 我的表格:

There are lots of items that I list in a bootstrap multi-select widget. 我在引导式多选小部件中列出了很多项目。 To save time, I added a checkbox to the form that will add the descendants of the selected 'items' to my 'event' as well. 为了节省时间,我在表单中添加了一个复选框,该复选框还将选择的“项目”的后代也添加到“事件”中。

addToChildren = forms.BooleanField(required=False, label="Add event to descendants of selected item")

My View: 我的观点:

Once the form is submitted, I add the descendants with the following code: 提交表单后,我将使用以下代码添加后代:

class EventUpdate(UpdateView):
    #declared model, formclass and success url, but left it off for the question

    def form_valid(self, form):
        event = form.save()
        if form.cleaned_data['addToChildren'] == True:  #add to children if told to do so.
            print("items before save")
            print(self.object.items.all())
            for x in range(0,form.cleaned_data['items'].count()):
                itm = form.cleaned_data['items'][x]
                for descendant in itm.get_descendants():
                    self.object.items.add(descendant)#deletes when updated
            print("items after save")
            print(self.object.items.all())
        return super(EventUpdate, self).form_valid(form)

My Problem: 我的问题:

The print statements clearly show that the descendants of the selected items in are getting added to self.object (my event object). 打印语句清楚地表明所选项目的后代已添加到self.object(我的事件对象)中。

The issue is that the return statement disregards my changes, adds selected items to my event, and removes all unselected items from my event. 问题是return语句忽略了我的更改,将所选项目添加到事件中,并从事件中删除了所有未选择的项目。 I would like that to stop. 我想停止。

Is there a way to append information to form.cleaned_data from a django view? 有没有一种方法可以将信息从django视图追加到form.cleaned_data?

In your method you save the object, then take the related instances in the items field and add their dependents. 在您的方法中,保存对象,然后在items字段中获取相关实例并添加其依赖项。 But when you call the super method, Django will again save the form - including the many-to-many related objects, but it won't know about the ones you added so will overwrite them with only the selected ones. 但是,当您调用super方法时,Django将再次保存该表单-包括许多相关的对象,但是它不知道您添加的对象,因此只会用选定的对象覆盖它们。

Generally your desired behaviour seems a bit strange, but you can preserve it by not calling the super method at all. 通常,您想要的行为似乎有些奇怪,但是您可以通过根本不调用super方法来保留它。 Apart from saving, the main thing the default implementation does is redirect to the success URL. 除了保存之外,默认实现的主要作用是重定向到成功URL。 So, you should replace that super call with a line that does that explicitly: 因此,您应该用明确执行此操作的行替换该超级调用:

return HttpResponseRedirect(self.get_success_url())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM