Django how to sequence number array or specified format

query the data in this way

    user_add1 = UserAddress.objects.filter(id=int(add_id))

then serialize

json_data = serializers.serialize("json", user_add1, ensure_ascii=False)

return json

return HttpResponse(json.dumps(json_data), content_type="application/json; charset=utf-8")

the front end receives json
how to remove [] parentheses?

[{"model": "Shopping.useraddress", "pk": 8, "fields": {"user": 3, "district": "", "address": "", "signer_name": "", "signer_mobile": "12345678901", "default_add": false, "add_time": "2018-03-29T14:32:35.552Z"}}]

how does the front end call json
alert (data.fields.address) to get no value

get the ID of the editing address and ajax it to the backend. For example, I click on the edit of id=9, but what I actually send back is how can id=7, return a different id?

<a onclick="modify()" id="modify" data-id="7" ><i class="am-icon-edit"></i></a>
<a onclick="modify()" id="modify" data-id="8" ><i class="am-icon-edit"></i></a>
<a onclick="modify()" id="modify" data-id="9" ><i class="am-icon-edit"></i></a>
function like() {
      var tree = document.getElementById("delete");
      var id = (tree.getAttribute("data-id"));
      $.ajax({
         cache: false,
         type: "POST",
         url:"{% url "address" %}",
         data:{"add_id": id, "type": "delete"},
         async: true,
         beforeSend:function(xhr, settings){
                 xhr.setRequestHeader("X-CSRFToken", "{{ csrf_token }}");
                            },
                            success: function(data) {
                                if(data.status == "success"){
                                    window.location.reload();
                                }
                        },
                    });
                }
Feb.28,2021

first of all, what you user_add1 = UserAddress.objects.filter (id=int (add_id)) gets is an "array-like" thing, just think of it as an array, and of course it's an array after you serialize it.

literally, id is unique, so instead of filter , you should get : user_add1 = UserAddress.objects.get (id=int (add_id)) , so you get a UserAddress object instead of an array. This is the second question.

the last question is that the front end gets something after serialization. To put it bluntly, you need to call JSON.parse to convert these strings into JSON objects. Search JSON.parse for more details.

then if you must use filter , the way to remove the brackets is also simple:

  1. take the objects out of the array before serialization and serialize the objects themselves instead of the array;
  2. removes the "[]" from the beginning and end of the string after serialization.
< hr >

use json.dumps instead of serializers.serialize :

first you need to implement the toJSON method, for example:

class UserAddress(model):
    ...
    def toJSON(self):
        return {
            'model': 'Shopping.useraddress',
            'pk': self.pk,
            'fields': {
                'user': 3,
                'district': self.user.district,
                'address': self.user.district,
                'signer_name': self.user.signer_name,
                'signer_mobile': self.user.signer_mobile,
                'default_add': self.user.default_add,
                'add_time': time.mktime(self.user.add_time.timetuple)
            }
        }

then

import json

...

user_add1 = UserAddress.objects.get(id=int(add_id))

json_data = json.dumps(user_add1.toJSON())

return HttpResponse(json_data, content_type="application/json; charset=utf-8")

or

user_add1 = UserAddress.objects.get(id=int(add_id))

json_data = json.dumps(user_add1)

return JsonResponse(user_add1.toJSON(), safe=False)
< hr >

first of all, the id in html must be unique, otherwise it is called id?. You have three a tags whose id is modify . Then the onclick of your a tag calls modify () , and what you give below is like () . Let's just say these two functions are one function. How can the parameter of document.getElementById in your like () become delete ? < del > ha < / del >

.

so all you have to do is change the id to a unique one, and then call modify () or like () via onclick , which raises a new question: how does the function called by onclick know which element you click ? The solution is to search for "onclick parameters".

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3212c-40372.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3212c-40372.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?