我正在使用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多种方式没有运气。有什么想法吗?
答案 0 :(得分:0)
为了澄清,我假设您看到显示的表单,您将其填入值并尝试提交。此时,您会看到表单验证错误,指示您需要填写隐藏字段。这是正确的吗?
如果是这样,您需要在提交之前在表单上设置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
答案 1 :(得分:0)
我发现您可以使用
设置表单值review_form.base_fields["content_type"] = ...
虽然这似乎仍然不适用于content_type。这实际上是我的错误。我打算在验证表单后实例化一个Review对象,并在该对象上设置content_type。更容易。