get last insert ID

2019-06-12 03:14发布

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

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

Now i want id of the file for further processing like

What i want is :-

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.

I tried:

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

So conclusion from above when each file upload happen 1 *id* is printing.

#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 ....

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

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

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.

1条回答
欢心
2楼-- · 2019-06-12 04:05

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

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

First write two view(request).

  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)

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)  
    });

so in the response you will get the id

查看更多
登录 后发表回答