django-UpdateView-如何影响不同的表

django-UpdateView-如何影响不同的表,django,overloading,overriding,django-class-based-views,Django,Overloading,Overriding,Django Class Based Views,我遇到了一个自己无法解决的问题:( 假设我有三张桌子: 1) 程序集,它只保存名称 |id|assembly_name| ------------------ | 1|assembly_1 | | 2|assembly_2 | | 3|assembly_3 | 2) 任务编号为的装配完成百分比(手动添加) 3) 商店-告诉我货架上有多少个混蛋 |id|assembly_id|qty| -------------------- | 1| 1| 1| | 2

我遇到了一个自己无法解决的问题:( 假设我有三张桌子: 1) 程序集,它只保存名称

|id|assembly_name|
------------------    
| 1|assembly_1   |
| 2|assembly_2   |
| 3|assembly_3   |
2) 任务编号为的装配完成百分比(手动添加)

3) 商店-告诉我货架上有多少个混蛋

|id|assembly_id|qty|
--------------------
| 1|          1|  1|
| 2|          2|  0|
| 3|          3|  0|
现在在我的索引页上有一个表,它显示了第二个表,不包括100%就绪。问题是,我想,使用updateView,设置给定任务的百分比(我可以这样做),但我还想在某些程序集达到100%时自动在3个表中添加数量。可能吗?我是否可以覆盖updateView方法,导入模型存储并添加如下内容:if instance.percentage==100%add 1 to store.qty(对于给定的程序集\u id)?
我的意思是:将表2(id=2)更新到100%后,表3中的数量将上升到2,当然,在您的
更新视图中覆盖
表单有效(self,form)
。 您可以保存模型并获取实例
instance=form.save()
,然后询问您想要什么
如果instance.percentage==100
,然后执行您想要的操作。只是别忘了
返回super(你的updateviewclass,self)。form\u valid(form)

另一方面,如果您不要求在请求百分比之前将实例保存在数据库中,则可以使用
instance=form.save(commit=False)
,这将在实例保存到数据库之前获取该实例。调用
super
方法可以做到这一点,因此您可能会为自己保存一个重复的更新查询

编辑:其外观示例:

class CustomUpdateView(UpdateView):
    ...
    ...
    def form_valid(self, form):
        instance = form.save(commit=False)
        # The previous line will return the instance, but won't
        # save it in database yet
        if instance.percentage == 100:
            ...do whatever other thing you want to do...
            # Here you can query other models and save them
            # as you stated in your question
        # And you finally return the inherited implementation
        # of form_valid, which will save the instance in
        # database and redirect to success_url (default behaviour)
        return super(CustomUpdateView, self).form_valid(form)

那么,我重写的方法是什么样子的:def form_valid(self,form):if form.percentage==1:store.qty[id]+=1 return super(MyUpdateViewClass,self)。form_valid(form)?不幸的是,未定义“我获取全局名称”表单:(
class CustomUpdateView(UpdateView):
    ...
    ...
    def form_valid(self, form):
        instance = form.save(commit=False)
        # The previous line will return the instance, but won't
        # save it in database yet
        if instance.percentage == 100:
            ...do whatever other thing you want to do...
            # Here you can query other models and save them
            # as you stated in your question
        # And you finally return the inherited implementation
        # of form_valid, which will save the instance in
        # database and redirect to success_url (default behaviour)
        return super(CustomUpdateView, self).form_valid(form)