django - UpdateView - 如何影响不同 table
django - UpdateView - how to affect different table
我遇到了一个我自己无法解决的问题:(
假设我有三个 table:
1) 仅包含名称的程序集
|id|assembly_name|
------------------
| 1|assembly_1 |
| 2|assembly_2 |
| 3|assembly_3 |
2) 任务号组装完成百分比(手动添加)
|id|assembly_id|percentage|current_task|
----------------------------------------
| 1| 1| 100|1232131 |
| 2| 1| 90|12131 |
| 3| 2| 80|1131 |
| 4| 3| 50|12241 |
3)store - 告诉我货架上有多少组合
|id|assembly_id|qty|
--------------------
| 1| 1| 1|
| 2| 2| 0|
| 3| 3| 0|
现在在我的索引页面上有一个 table,它显示第二个 table,不包括 100% 就绪。问题是,我想使用 updateView 设置给定任务的百分比(我可以做到),但我也想在某些程序集达到 100% 时自动添加 3 table 中的数量。那可能吗 ?我可以覆盖 updateView 方法,导入模型存储并添加如下内容:if instance.percentage == 100% add 1 to store.qty for given assembly_id 吗?
我的意思是:更新 table2 (id=2) 到 100% 后 table 3 中的数量将增加到 2
当然,在您的 UpdateView
中覆盖 form_valid(self, form)
。
你可以保存模型并获取实例 instance = form.save()
然后询问你想要什么 if instance.percentage == 100
然后做你想做的。只是不要忘记 return super(YourUpdateViewClass, self).form_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)
我遇到了一个我自己无法解决的问题:( 假设我有三个 table: 1) 仅包含名称的程序集
|id|assembly_name|
------------------
| 1|assembly_1 |
| 2|assembly_2 |
| 3|assembly_3 |
2) 任务号组装完成百分比(手动添加)
|id|assembly_id|percentage|current_task|
----------------------------------------
| 1| 1| 100|1232131 |
| 2| 1| 90|12131 |
| 3| 2| 80|1131 |
| 4| 3| 50|12241 |
3)store - 告诉我货架上有多少组合
|id|assembly_id|qty|
--------------------
| 1| 1| 1|
| 2| 2| 0|
| 3| 3| 0|
现在在我的索引页面上有一个 table,它显示第二个 table,不包括 100% 就绪。问题是,我想使用 updateView 设置给定任务的百分比(我可以做到),但我也想在某些程序集达到 100% 时自动添加 3 table 中的数量。那可能吗 ?我可以覆盖 updateView 方法,导入模型存储并添加如下内容:if instance.percentage == 100% add 1 to store.qty for given assembly_id 吗? 我的意思是:更新 table2 (id=2) 到 100% 后 table 3 中的数量将增加到 2
当然,在您的 UpdateView
中覆盖 form_valid(self, form)
。
你可以保存模型并获取实例 instance = form.save()
然后询问你想要什么 if instance.percentage == 100
然后做你想做的。只是不要忘记 return super(YourUpdateViewClass, self).form_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)