获取最后一个插入ID [英] get last insert ID

查看:111
本文介绍了获取最后一个插入ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以问题是如何在view函数尝试 save()对象时获取id?

So the question is how to get the id when view function try to save() the object.?

我正在使用dropzonejs将文件上传到服务器。每当用户拖动&删除该文件,而不等待它会将文件发送到服务器,这是正常工作,但是

I am using dropzonejs for uploading file to the server. Whenever User drag & drop the file without waiting it will sent the files to server, this is working fine but

现在我想要 id

Now i want id of the file for further processing like

我想要的是: -

如果用户选择两个或三个文件,请选中该复选框选项,然后单击下一步,并将渲染页面列出所有选定的文件以进行保存标题编辑等。甚至保存外键。

If User select two or three files in choose the check box option and click next and the render page i want to list all the selected files for saving title or editing etc .. even saving the foreign key also.

我试过:

new_file = Tracks(file = request.FILES['file'])
new_file.save()
pprint.pprint(new_file.id)

控制台日志的输出是

Output of the console log is

[11/Sep/2014 08:56:34] "GET /app/track-info HTTP/1.1" 200 10112
9L
[11/Sep/2014 08:56:42] "POST /app/upload-tracks HTTP/1.1" 302 0
[11/Sep/2014 08:56:42] "GET /app/upload-tracks HTTP/1.1" 200 9580
10L
[11/Sep/2014 08:57:20] "POST /app/upload-tracks HTTP/1.1" 302 0

所以从上面的结论当每个文件上传发生1 * id *正在打印

#upload.html
<div class="tb-column col-5"><p class="name" data-dz-name></p></div>
<div class="tb-column col-2"><p class="size" data-dz-size></p></div>
<div class="select"><input type="checkbox" class="checkBox" value="???" id="check"></div>

所以从我想要实现的,我只需要复选框的值....

So from what i want to achieve, i just need the value of the check box ....

如何识别特定的HTML元素,基于输出来自控制台日志(如9L,10L等)

how to identify the particular HTML element, based on the output comes from console log (like 9L, 10L etc ..)

我如何写入 value =???,它会自动获取文件ID。

how can i write value="???" which take automatically ID of the file .?

我问不同的问题昨天和一些与这个有关的东西,但是我将文件存储到db中有困难,所以我放弃了这个想法。

I asked different question yesterday & some what related to this one but i had the difficulty in storing the file into db, so i dropped that idea.

推荐答案

我想你把保存文件的视图写入db?

I guess you wrote the view like save file into db ?

那么这里是答案将帮助你最后插入 id 在html页面

then here is the answer will help you to get last inserted id in html page

首先写两个视图(请求)。

First write two view(request).


  1. 用于呈现页面模板和上下文。

  2. 第二个视图是其中文件处理并获取 id

  1. For rendering page template and context.
  2. Second view is for where file handling and get the id

like:

def view_second(request)
        if request.method == 'POST':
            if form valid:
                #get file here
                new.save()
                msg = new.id
            return HttpResponse(msg)

就是这样,现在在你的html之后的dropzone脚本..

And that's it, Now in your html after the dropzone script ..

Dropzone.options.myDropzone = {
  init: function() {
    this.on("success", function(file, response) {
      #alert(response)  
    });

所以在回复中你会得到 id

so in the response you will get the id

这篇关于获取最后一个插入ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆