我对django很新,但意识到这种表单处理非常冗长。有没有办法缩短这样的陈述?我查看了ModelForms,但我不确定它是否适用于此(也许我错了?)。
if 'solve_comment' in request.POST:
ticket.state_id = 5
text = request.POST['solve_comment']
comment.text = text + " (descended back to Level 1)"
ticket.group_id = 6
comment.comment_type = "solving_note"
comment.content_type = ContentType.objects.get(id=114)
comment.object_id = ticket.id
comment.created_by_id = request.user.id
comment.save()
ticket.save()
if 'closing_note' in request.POST:
ticket.state_id = 6
text = request.POST['closing_note']
ticket_issue = request.POST['ticket_issue']
comment.text = text + (" (%s)" %ticket_issue)
comment.comment_type = "closing_note"
comment.content_type = ContentType.objects.get(id=114)
comment.object_id = ticket.id
comment.created_by_id = request.user.id
comment.save()
ticket.save()
if 'private_note' in request.POST:
text = request.POST['private_note']
comment.text = text
comment.comment_type = "private_note"
#this line needs to be fixed
comment.content_type = ContentType.objects.get(id=114)
comment.object_id = ticket.id
comment.created_by_id = request.user.id
comment.save()
if 'reopen-ticket-button' in request.POST:
ticket.state_id = 2
ticket.save()
if 'hold-ticket-button' in request.POST:
ticket.state_id = 4
ticket.save()
if 'take_ownership' in request.POST:
ticket.assignee_id = request.user.id
if ticket.state_id == 1:
ticket.state_id = 2
ticket.save()