python - Django - Export CSV in ManytoMany Field
问题描述
How can I export the name of a category on M2M a relationship? I've tried to use .all() but it returns everything without format, so it is not useful
#----------
class Category(models.Model):
name = models.CharField(verbose_name='Name', max_length=254)
class SubCategory(models.Model):
name = models.CharField(verbose_name='Name', max_length=254)
category = models.ManyToManyField(Category, blank=True)
#----------
from django.http import HttpResponse
import csv
def csv_export(request):
# Get database information
qs = SubCategory.objects.all()
# Create the HttpResponse object with the appropriate CSV header.
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="export_file.csv"'
writer = csv.writer(response)
writer.writerow(['name','category'])
for rule in qs:
writer.writerow(['name','category'])
return response
解决方案
A .csv
files is basically a table. So it is not straightforward to serialize a many-to-many relation. If a SubCategory
has two categories, then how will you serialize this?
You can serialize all combinations of subcategories/categories. So then the .csv
file looks like:
subcat1,cat1|cat2|cat3
subcat2,cat2|cat4
subcat3,cat1
a potential problem with this is that if the category name contains a pipe character (|
), then you can not make a distinction between foo|bar
as two Category
s foo
and bar
, or a category with name foo|bar
.
from django.http import HttpResponse
import csv
def csv_export(request):
# Get database information
qs = SubCategory.objects.prefetch_related(
'category'
)
# Create the HttpResponse object with the appropriate CSV header.
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="export_file.csv"'
writer = csv.writer(response)
writer.writerow(['name','category'])
for rule in qs:
writer.writerow(
[rule.name,'|'.join(c.name for c in rule.category.all())]
)
return response
推荐阅读
- javascript - 自 M71 以来,不再允许没有用户激活的 JavaScript speechSynthesis.speak()
- nginx - 如何在向客户端发送响应时修复“失败(22:无效参数)”
- javascript - 在页面加载以及 setInterval 上运行 JavaScript 函数
- r - 如何使用 tidyverse 将长形转换为宽形
- algorithm - 硬币变化问题的变化背后的想法是什么?
- node.js - Node js 逐行模块
- php - 如何在这个 sql 查询中获得前 5 种食物
- c# - 如何解决从@Html.CheckBoxFor 自动生成的隐藏表单字段
- image - Xamarin 表单:基于字符串值隐藏 Flowlistview 项目,包括它在 UI 中的空间
- sql - 我想优化这个查询