如何缩小Django DetailView导出到CSV文件 [英] How To Narrow Down Django DetailView Export To CSV File

查看:99
本文介绍了如何缩小Django DetailView导出到CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试弄清楚如何将数据简单导出到CSV文件。我找到了一个有效的示例。...

I am trying to figure out how to do a simple export of data to a CSV file. I found an example that works....

def export_data(request):
    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename="users.csv"'

    writer = csv.writer(response)
    writer.writerow(['username', 'First name', 'Last name', 'Email address'])

    users = User.objects.all().values_list('name','userid')
    for user in users:
    writer.writerow(user)

    return response

上面的代码可以正常工作,导出所有从User.objects.all()到电子表格的用户数量。但是,我试图从DetailView中执行此操作,并且仅使用.all()获取正在查看的用户的数据,而不是整个模型的数据。

The code above works as you would expect, exporting all of the users from User.objects.all() to the spreadsheet. However, I am trying to do this from a DetailView and only get the data for the user that is being viewed, not the entire model, with the .all().

从我收集的数据来看,为了在DetailView中进行此操作,我相信我需要做类似的事情...

From what I gather, in order to do this in a DetailView, I believe I need to do something like...

class ExportDataDetailView(LoginRequiredMixin,DetailView):
    model = Author
    context_object_name = 'author_detail'

...然后我可能需要重写get_queryset吗?不过,这似乎有些矫kill过正,因为我已经在DetailView中了。

....And then perhaps I need to override get_queryset? It seems like overkill though because I'm already in the DetailView...

感谢任何提前向正确方向指向的指针。

Thanks for any pointers in the right direction in advance.

推荐答案

使用 DetailView 时,意味着只能使用对象。如果要导出,则可以执行以下操作:

When you are using DetailView, means you can use only object. If you want to export that, you can do the following:

class ExportDataDetailView(LoginRequiredMixin,DetailView):

    model = Author
    context_object_name = 'author_detail'

    def render_to_response(self, context, **response_kwargs):
        user = context.get('author_detail')  # getting User object from context using context_object_name
        response = HttpResponse(content_type='text/csv')
        response['Content-Disposition'] = 'attachment; filename="users.csv"'
        writer = csv.writer(response)
        writer.writerow(['username', 'First name', 'Last name', 'Email address'])
        writer.writerow([user.username, user.first_name, ...])
        return response

现在,如果要获取所有用户的数据,则可以使用 ListView

Now, if you want all users' data, then you can use ListView:

from django.views.generic.list import ListView

class ExportDataListView(LoginRequiredMixin,ListView):
    model = Author
    context_object_name = 'authors'

    def render_to_response(self, context, **response_kwargs):
        authors = context.get('authors')  # getting all author objects from context using context_object_name
        users = authors.values_list('name','userid', ...)
        response = HttpResponse(content_type='text/csv')
        response['Content-Disposition'] = 'attachment; filename="users.csv"'
        writer = csv.writer(response)
        writer.writerow(['username', 'First name', 'Last name', 'Email address'])
        for user in users:
           writer.writerow(user)
        return response

这篇关于如何缩小Django DetailView导出到CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆