Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

332
Vistas
How to get model objects with AJAX?

I have a Customer model in my Django application. New users are constantly being added to this model in the background and I want to list these customers dynamically with AJAX. But I cannot figure it out. How can I do that?

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>

I try to do that but It gives an error:

TypeError: Object of type Customer is not JSON serializable

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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>
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda