您好我有一个编辑项目表单。在此视图中,请查看item.current_item_status_date
。
def edit_item(request, client_id = 0, item_id = 0):
client = None
item = None
status = None
contact = None
status_id = request.POST.get('status_id', None)
contact_id = request.POST.get('contact_id', None)
save_item = request.POST.get('save_item', None)
save_status = request.POST.get('save_status', None)
try:
client = models.Client.objects.get(pk = client_id)
item = models.StorageItem.objects.get(pk = item_id)
except:
return HttpResponseNotFound()
try:
status = models.Status.objects.get(pk = status_id)
contact = models.Contact.objects.get(pk = contact_id)
except:
pass
if request.method == 'POST':
form = forms.ItemForm(request.POST, instance = item)
if form.is_valid() and save_item is not None:
item.current_item_status_date = date.today()
item.save()
form.save(True)
request.user.message_set.create(message = "Item {0} has been updated successfully.".format(item.tiptop_id))
return HttpResponse("<script language=\"javascript\" type=\"text/javascript\">window.opener.location = window.opener.location; window.close();</script>")
if status is not None and contact is not None and save_status is not None:
current_status = models.ItemStatusHistory(item = item, contact = contact, status = status,
user = request.user)
current_status.save()
request.user.message_set.create(message = "Item status has been updated successfully.")
else:
form = forms.ItemForm(instance = item)
title = str(client) + ' : Edit Item'
status_list = models.Status.objects.all()
return render_to_response('edit_item.html', {'form':form, 'title':title, 'status_list':status_list, 'item':item}, context_instance = RequestContext(request))
current_item_status_date会在提交表单时保存今天的日期。但是,存在一个小问题。在我的表单中,我只想在按钮名为新状态时保存 被选中。现在我的编辑项目表单正在执行的是当选择保存按钮时保存它。我希望我的表单仅在选择新状态时保存此日期 - 而不是保存。
以下是我的编辑订单模板中的内容。
<td><input type="submit" name="save_status" value="new status"></td>
<input type="submit" name="save_item" value="Save" onclick="validate_item(this.form)">
编辑:我已完成这些更改。
#code
if request.method == 'POST':
form = forms.ItemForm(request.POST, instance = item)
if form.is_valid() and save_item is not None:
form.save(True)
request.user.message_set.create(message = "Item {0} has been updated successfully.".format(item.tiptop_id))
return HttpResponse("<script language=\"javascript\" type=\"text/javascript\">window.opener.location = window.opener.location; window.close();</script>")
if request.POST.get('save_status'):
item.current_item_status_date = date.today()
item.save()
#code
现在,通过执行此操作,将保存当前项目状态日期。但只有我关闭表单(这是一个打开新窗口的表单)。选择状态日期然后单击“保存”时,它不会保存。我认为它没有正确地保存到current_item_status_date字段中,而不是数据库中。当我点击保存状态时,我可以告诉它不起作用,字段current_item_status不会更新。它仅在我关闭表单并再次打开时更新。唯一可行的方法是表单重新加载从数据库中获取信息。
这就是我要找的:
答案 0 :(得分:0)
您正在专门检查save_item。将其更改为save_status,如果我理解您的问题,您应该没问题。
if form.is_valid() and save_status is not None:
答案 1 :(得分:0)
从current_item_status_date
删除form = forms.ItemForm
字段似乎有效。这样就可以妥善保存。