[英]How do I get the value of a field from an object that has been recently created in Django
I am trying to pass the primary key of a service object so that I can list all of the fields by redirecting to an html page. 我试图传递服务对象的主键,以便可以通过重定向到html页面列出所有字段。 I am not sure how to get the value of the primary key of the service object that has just been created.
我不确定如何获取刚刚创建的服务对象的主键的值。
Below is my view.py 下面是我的view.py
def create(request):
# val= 3
if request.POST:
form= ServiceForm(request.POST)
if form.is_valid():
form.save()
return render_to_response('services/service_created.html',
{'service_id': request.POST.id}) # THIS CODES IS INCORRECT
else:
form = ServiceForm()
args= {}
args.update(csrf(request))
args['form'] = form
return render_to_response('services/create_service.html', args )
Method save
of the form returns created object. 表单的方法
save
返回创建的对象。 So you can get ID of object: 这样就可以得到对象的ID:
service_obj = form.save()
...
{'service_id': service_obj.id}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.