Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

188
Views
Getting all the objects in the JavaScript from Django

I want to get all my object models from the database and store them in someway, the model I have is:

class Device (models.Model):
    patientId = models.IntegerField(unique=True)
    deviceId = models.CharField(unique=True,max_length=100)
    hour = models.DateTimeField()
    type = models.IntegerField()
    glucoseValue = models.IntegerField()

I'm sending them in views.py:

device_list = list(Device.objects.all())
context = {"filename": filename,
           "deviceList": device_list,
               }

In JS I managed to get each object like that:

{% for item in deviceList%}
console.log( "{{item}}" );
{% endfor %}

What I want is to store those objects in some way, but I don't really know how, because they are coming like that Model object (1).

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You have to convert django model to javascript object, you can do it in many way, but I recommend you something like this:

in views.py convert object to json dict:

"deviceList": json.dumps([model_to_dict(x) for x in device_list])

In HTML template you can do something like this:

<script>
const deviceList = JSON.parse('{{ deviceList|safe }}')
console.log(deviceList)
</script>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!