JSON files. How is it done?

49 views
Skip to first unread message

Matheus Bon

unread,
Sep 22, 2022, 6:23:11 AM9/22/22
to Django users
Hi!

I'm lost in a question of my project, I see so many methods on the internet but none work for me (maybe I'm implementing it wrongly)

But, I would like to know if you could give me a method to transform my model into a JSON file and then be able to retrieve these JSON files for me to put in an INPUT with a dropdown, using, for example, SELECT2.

Thanks!

Thomas Lockhart

unread,
Sep 22, 2022, 1:50:53 PM9/22/22
to django...@googlegroups.com
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.

Julio Cojom

unread,
Sep 22, 2022, 3:41:46 PM9/22/22
to django...@googlegroups.com
Hi Matheus! 

Try to use django-select2 it is easy to set up and works like a charm! 

It has an advanced and easy implementation, so try first with the easy ones and then if your project needs something heavier, you can refactor the code with a simple couple of lines.


Regards,

Julio Cojom

--

Matheus Bon

unread,
Sep 23, 2022, 3:45:56 PM9/23/22
to django...@googlegroups.com
Thank you all!

You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/oQCdaU1cFfA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHRQUHmdjwLux9ok-WVOYFon59HhoFhyXEXbCg0kP9KBSuVpLA%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages