2👍
MyModel.objects.values_list('date', 'value', flat=True)
1👍
You can use list comprehension:
[(str(my_record.date), str(my_record.value)) for my_record in MyModel.objects.all()]
In fact, this way you can transform the data however you wish.
- [Django]-How to put absolute url field in serializer model in django rest framework?
- [Django]-Better django model design
- [Django]-Fetch data of Android POST in django server
- [Django]-Django – Forms – change field names on template
0👍
import calendar
import json
raw_data = MyModel.objects.values_list('date', 'value')
result = []
for date, value in raw_data:
result.append([calendar.timegm(date.timetuple()) * 1000,
float(value)])
To use result in template, serialize it to JSON first and pass data
to template
data = json.dumps(result)
In template
var data = {{ data|safe }};
Also:
- [Django]-Django: passing a url as a parameter
- [Django]-.js files not included in page when using Django Form Assets (Media class) with crispy forms
Source:stackexchange.com