i want put link in django app download app's data mysql server csv format. there easy way that?
correction: when i'm saying database, meant related tables 1 file, means need join them before downloading. since i'm doing client , not need see tables including log , user tables. , can use 1 file basic reporting instead of entire relational database. so, first need denormalize relational database , make ready downloaded.
somewhat duplciate of this: django download csv file using link
you this:
from io import stringio django.core import management def create_fixture(app_name, filename): buf = stringio() management.call_command('dumpdata', app_name, stdout=buf) buf.seek(0) open(filename, 'w') f: f.write(buf.read()) class yourpage(...): .... def dispatch(self, *args, **kwargs): create_fixture('<yourapp>', '<yourapp>/static/reports/test.csv') return super(yourpage, self).dispatch(*args, **kwargs)
then in view, (assuming configured static file paths correctly)
{% load staticfiles %} <a href="{% static '<yourapp>/test.csv' %}">download csv</a>
obviously don't data dump inside view logic, slow stuff down, might want doing async elsewhere in code base.
Comments
Post a Comment