如何使用 AJAX 获取模型对象?

How to get model objects with AJAX?

我的 Django 应用程序中有一个 Customer 模型。新用户在后台不断添加到此模型,我想用 AJAX 动态列出这些客户。但我想不通。我该怎么做?

def ajax_view(request):
    obj = Customer.objects.filter(company=request.user.company)
    customers = []
    for customer in obj:
        customers.append(customer)
    data = {
        "obj": customers
    }
    return JsonResponse(data)

html

<h3 id="status"></h3>
<script>
        var refresh = setInterval(function() {$.ajax({
            url: "{% url 'user:ajax_customer' %}",
            type: "GET",
            dataType: 'json',
            success: function (data) {
                if (data) {
                    console.log(data);
                    console.log(status);
                    document.getElementById("status").innerHTML = data.obj;
                }

            }

        });}, 1000);
    </script>

我尝试这样做,但出现错误:

TypeError: Object of type Customer is not JSON serializable

def ajax_view(request):
    obj = list(Customer.objects.filter(company=request.user.company).values())
    return JsonResponse({'data':obj})

JQUERY

<script>
    var refresh = setInterval(function() {$.ajax({
        url: "{% url 'user:ajax_customer' %}",
        type: "GET",
        dataType: 'json',
        success: function (data) {
            let html = ''
            data.data.forEach((element, index) => {
               html += `<tr>
                    <td>${element.columname}</td>
                    <td>${element.columnname}</td>
                    <td>${element.columnname}</td>
               <tr>`
            })
            $('#status').html(html)
        }
    });}, 1000);
</script>

HTML

    <table>
      <thead>
         <tr>
            <td>columname</td>
            <td>columnname</td>
            <td>columnname</td>
         </tr>
      </thead>
      <tbody id="status">
      </tbody>
    </table>