简体   繁体   English

如何从Django最近创建的对象中获取字段的值

[英]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.

相关问题 如何找出Django中是否创建了新对象? - How to find out if there has been new object created in Django? Python-如何从对象列表中获取其date字段具有最新值的对象? - Python- how do I get the object whose date field has the most recent value from a list of objects? 获取最近创建的Django Rest Framework对象的id - Get the id of the object recently created Django Rest Framework 如何在 Python/Django 中检查字段的值是否已被删除 - How to check if the value of a field has been deleted in Python/ Django 如何存储从用户输入创建的变量并在 Python 的不同文件中使用它? - How do I store a variable that has been created from a user input and use it in a different file in Python? 如何使用 django 模板从 model 的父 object 获取字段? - How do I use django templates to get a field from the parent object of a model? Django的变化领域,这是由额外的创建 - Django changing field which has been created by extra 如何从 Django 中创建的 object 获取 id - How to get id from created object in Django Django-如何获取对象的十进制字段的值 - Django - How to get the value of a decimal field of object 如何获取 Django 模型字段对象的值 - How to get the value of a Django Model Field object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM