![](/img/trans.png)
[英]How do I delete a single instance of an intermediate model in a Django Many-to-many (through) relationship?
[英]How to bundle many forms in single instance in django
主要問題是是否有任何方法可以將許多django表單捆綁到單個實例中,以弄清楚我需要解釋的問題:
我創建了一堆表單類,這些表單類需要一起工作才能顯示單個視圖。
from_form = move_forms.WaypointForm(prefix="marker-from", instance=move.from_place)
to_form = move_forms.WaypointForm(prefix="marker-to", instance=move.to_place)
#Notice that last two form are of the same class
through = ThroughFormset(prefix="through", queryset=move.waypoints_db.all())
path_form = move_forms.CarMovePathForm(path=move.path)
date_form = move_forms.MoveForm(instance=move)
#put all this into context instance and render
所有這些形式基本上都顯示/編輯相同的數據庫實例---但可以在應用程序中以不同的配置重復使用(因此,我不能只手動創建將其封裝的類)。
網頁中有許多表格是很麻煩的,例如,我必須編寫如下代碼:
if transportation_form.is_valid() and from_form.is_valid() and \
to_form.is_valid() and through.is_valid() and path_form.is_valid():
我無法將表單屬性完全傳遞給視圖,因為大多數viev都以這種方式使用許多表單。
是否有任何明智的方式來捆綁這些表格---還是我的設計壞了(如果可以的話,如何解決)。
那這個呢
from_form = move_forms.WaypointForm(
request.POST or None,
prefix="marker-from",
instance=move.from_place)
# ... other forms declared the same way
forms = {
'from_form': from_form,
'to_form': to_form,
# ...
}
if all(f.is_valid() for f in forms.values()):
# ...
return redirect('success')
return render(request, 'template.html', {'forms': forms})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.