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).
CodePudding user response:
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>
