This will create a JSON file of the specified name for data from a time range specified in the URL.
Re-reading your post, it seems that perhaps you don’t actually want a physical file but rather some JSON data to populate your browser window.Then Django Rest Framework will do what you want after you add in some javascript to manage the queries and populate the fields.
To be able to actually help I think you need to post more detailed questions, either to get you started or to get things working.
hth
- Tom
class ExampleDumpView(View):
"""
Provide data in a downloaded CSV file.
"""
model = ExampleModel
prefix = “example"
class Echo:
"""
An object that implements just the write method of the file-like interface.
"""
def write(self, value):
"""
Write the value by returning it, instead of storing in a buffer.
"""
return value
pass
def get(self, request, start, stop):
"""
Stream a (possibly) large file in CSV format.
"""
# Generate a sequence of rows.
(stime, etime) = times_from_ints(start, stop)
data = self.model.objects.within(stime, etime).order_by("time")
# assemble the rows
rows = []
rows.append(["time", “value"])
for entry in data:
rtime = entry.time
value = entry.value
row = [format_datetime(rtime), str(value)]
rows.append(row)
pass
# write out using example from the Django CSV HowTo...
fpath = path_from_times(self.prefix, stime, etime)
pseudo_buffer = self.Echo()
writer = csv.writer(pseudo_buffer)
response = StreamingHttpResponse((writer.writerow(row) for row in rows), content_type="text/csv")
response["Content-Disposition"] = f"attachment; filename=\"{fpath}\""
return response
pass
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
django-users...@googlegroups.com.
> To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/d89cac37-fdb7-436d-8e67-3e848ff425b3n%40googlegroups.com.