[英]Django-reviews: cannot set content-type
我正在使用django-reviews(http://code.google.com/p/django-reviews/),並且無法設置評論的內容類型。 簡單的例子:
def check_review(request):
if request.method == 'POST':
reviewed_item = get_object_or_404(MyModel, pk=request.POST['object_pk'])
review_form = ReviewForm(target_object=reviewed_item, data=request.POST)
review_form.content_type = ContentType.objects.get_for_model(MyModel)
但是,表單的“ content_type”字段沒有值,並且表單具有錯誤“((隱藏字段content_type)此字段是必需的。”)。 我嘗試過多種方法來設置content_type,但是沒有運氣。 有任何想法嗎?
為了澄清,我假設您看到顯示的表單,將其填寫在值中並嘗試提交。 此時,您會看到表單驗證錯誤,指示您需要填寫隱藏字段。對嗎?
如果是這樣,則需要在提交之前在表單上設置content_type。 通常我會執行以下操作:
def check_review(request):
if request.method == 'POST':
reviewed_item = get_object_or_404(MyModel, pk=request.POST['object_pk'])
review_form = ReviewForm(target_object=reviewed_item, data=request.POST)
if review_form.is_valid():
# do some processing here
else:
# We're just getting an unbound form
reviewed_item = get_object_or_404(MyModel, pk=request.POST['object_pk'])
review_form = ReviewForm(target_object=reviewed_item, data=request.POST)
review_form.content_type = ContentType.objects.get_for_model(MyModel)
# return with review_form in the template's context or what have you
我發現您可以使用
review_form.base_fields["content_type"] = ...
雖然這似乎仍然不適用於content_type。 這實際上是我的錯誤。 我的意思是在驗證表單后實例化Review對象,並在該對象上設置content_type。 容易得多。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.