我需要在发布后获取pk的对象并保存表单。可以帮帮我吗? 视图:
def form_valid(self, form):
new_ticket = form.save()
address = form.cleaned_data['address'].encode('utf8')
title = form.cleaned_data['title'].encode('utf8')
recipient = form.cleaned_data['email'].encode('utf8')
name = form.cleaned_data['name'].encode('utf8')
room = form.cleaned_data['room'].encode('utf8')
return super(CreateTicket, self).form_valid(form)
答案 0 :(得分:2)
new_ticket
对象具有pk
,因为您已通过form.save()
所以你只需要这样做:
new_ticket_id = new_ticket.pk
获取对象的id字段
此外,您无需再次阅读表单属性。
address = new_ticket.address.encode('utf8')
#.. and so on.