我想用「實例」參數初始化一個對象,但它沒有進入窗體。這是必需的,所以is_valid失敗。有人可以請指教,我幾乎肯定這是一個容易的錯誤,但無法發現它。ModelForm「實例」參數和外鍵字段
FORM:
from django.forms import ModelForm
from tagging.forms import TagField
class BusinessEditForm(ModelForm):
tags = TagField()
class Meta:
model = Business
exclude = ('owner', 'pub_date')
邏輯在views.py
if businessid:
b = Business.objects.get(id=businessid)
category = b.category
assert(b.owner == request.user) or request.user.is_staff
#form = forms.BusinessEditForm(request.POST, instance=b)
else:
assert category.is_public or request.user.is_staff
b = Business(owner=request.user, category_id=category.id)
# form = forms.BusinessEditForm(request.POST, instance=b)
isNew = True
if request.method == "POST":
form = forms.BusinessEditForm(request.POST, instance=b)
if form.is_valid():
最後一行證實爲假。
任何幫助,歡迎。
感謝, 伊戈爾
如果你把'BusinessEditForm'類(它是否繼承自ModelForm?)會有所幫助。 – 2012-01-16 08:45:10
感謝,原始消息中更新,但基本上答案是肯定的) – 2012-01-16 09:08:35