如何将 Django 模型数据导出到 CSV 文件中
Posted
技术标签:
【中文标题】如何将 Django 模型数据导出到 CSV 文件中【英文标题】:How to export Django model data into CSV file 【发布时间】:2013-09-12 04:10:45 【问题描述】:我想将我所有的模型数据导出到 CSV 文件中:
models.py
import ast
import uuid
import base64
from django.db import models
from django.contrib import admin
from qlu.settings import HOST_NAME,STATS_URI
from django.core.validators import URLValidator
#------------------------------------------------------------------------------
class short_url(models.Model):
"""
This is a short_url class
"""
blocked = models.BooleanField(default=False) # To check whether URL is blocked or not
updated_at = models.DateTimeField(auto_now=True) # When URL is updated
url = models.TextField(validators=[URLValidator()]) # URL entered by the user
created_at = models.DateTimeField(auto_now_add=True) # When URL is created
url_hash = models.CharField(max_length=10,unique=True,db_index=True) # base64 encoded URL id
def _generateShortUrl(self):
"""
This function will generate base64 encoded URL hash
"""
hash = base64.urlsafe_b64encode(uuid.uuid1().bytes)[:6]
hash_exist = short_url.objects.filter(url_hash=hash)
while hash_exist:
hash = base64.urlsafe_b64encode(uuid.uuid1().bytes)[:6]
hash_exist = short_url.objects.filter(url_hash=hash)
continue
return hash
def save(self, *args, **kwargs):
"""
Custom Save method for link model
"""
self.url_hash = self._generateShortUrl()
super(short_url, self).save(*args, **kwargs)
def get_short_url(self):
"""
This method returns the url_hash related to the url
"""
return HOST_NAME + self.url_hash
def get_stats_url(self):
"""
This method returns the stats page URL for a url_hash
"""
return HOST_NAME + self.url_hash + STATS_URI
def __unicode__(self):
"""
This method convert Django model object to the user readable string
"""
return unicode(self.url)
class click_info(models.Model):
"""
This is a click_info class
"""
user_ip = models.TextField() # Store the user_ip
user_agent = models.TextField() # Store the user_agent
http_refrer = models.TextField() # Store the http_refrer
hash = models.ForeignKey(short_url) # base64 encoded URL id
get_parameters = models.TextField() # Store other get_parameters
request_time = models.DateTimeField() # When user made the request_time
updated_at = models.DateTimeField(auto_now=True) # When click_info is updated
created_at = models.DateTimeField(auto_now_add=True) # When click is created
def get_parameters_dict(self):
"""
This method returns the get parameter dict
"""
return ast.literal_eval(self.get_parameters)
def __unicode__(self):
"""
This method convert Django model object to the user readable string
"""
return unicode(self.hash)
#------------------------------------------------------------------------------
class short_url_admin(admin.ModelAdmin):
"""
short_url_admin class
"""
list_display = ('url','blocked','updated_at',
'created_at','url_hash')
exclude = ('url_hash',)
class url_info_admin(admin.ModelAdmin):
"""
url_info_admin class
"""
list_display = ('user_ip','user_agent','http_refrer',
'hash','request_time','get_parameters_dict')
#------------------------------------------------------------------------------
admin.site.register(short_url,short_url_admin)
admin.site.register(click_info,url_info_admin)
最好的方法是什么..?
【问题讨论】:
您可能应该阅读docs.djangoproject.com/en/dev/howto/outputting-csv。将它与 .objects.all() 结合起来,我认为你有解决方案。 我创建了一个基于类的视图来帮助在 python github.com/dm03514/django-cbv-toolkit 中组织 CSV 视图 github.com/azavea/django-queryset-csv @ChemicalProgrammer 谢谢 【参考方案1】:这是@symbiotech 对Python 3
的回答的更新版本。
灵感来自 this snippet 和 this article
utils.py
def download_csv(request, queryset):
if not request.user.is_staff:
raise PermissionDenied
model = queryset.model
model_fields = model._meta.fields + model._meta.many_to_many
field_names = [field.name for field in model_fields]
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="export.csv"'
# the csv writer
writer = csv.writer(response, delimiter=";")
# Write a first row with header information
writer.writerow(field_names)
# Write data rows
for row in queryset:
values = []
for field in field_names:
value = getattr(row, field)
if callable(value):
try:
value = value() or ''
except:
value = 'Error retrieving value'
if value is None:
value = ''
values.append(value)
writer.writerow(values)
return response
views.py
def export_csv(request):
# Create the HttpResponse object with the appropriate CSV header.
data = download_csv(request, Publication.objects.all())
response = HttpResponse(data, content_type='text/csv')
return response
【讨论】:
【参考方案2】:您好,您可以这样做
views.py
from django.http import HttpResponse
import csv
def export_users_csv(request):
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="users.csv"'
writer = csv.writer(response)
writer.writerow(['employee','IG', 'follower', 'email', 'website', 'DA', 'youtube_url', 'youtube_name', 'subscriber', 'type','country'])
users = Library.objects.all().values_list('employee','IG', 'follower', 'email', 'website', 'DA', 'youtube_url', 'youtube_name', 'subscriber', 'type','country')
for user in users:
writer.writerow(user)
return response
urls.py
from .views import export_users_csv
path('export', export_users_csv, name='export_users_csv'),
page.html
<a href="% url 'export_users_csv' %">Export all users</a>
【讨论】:
【参考方案3】:调整了一行。为避免返回 AttributeError 异常,如果该属性不存在,您可以添加默认值 None。
writer.writerow([getattr(obj, field, None) for field in field_names])
【讨论】:
【参考方案4】:我通常更喜欢在管理员中执行此操作。这是snippet:
def download_csv(modeladmin, request, queryset):
if not request.user.is_staff:
raise PermissionDenied
opts = queryset.model._meta
model = queryset.model
response = HttpResponse(mimetype='text/csv')
# force download.
response['Content-Disposition'] = 'attachment;filename=export.csv'
# the csv writer
writer = csv.writer(response)
field_names = [field.name for field in opts.fields]
# Write a first row with header information
writer.writerow(field_names)
# Write data rows
for obj in queryset:
writer.writerow([getattr(obj, field) for field in field_names])
return response
download_csv.short_description = "Download selected as csv"
在视图函数中使用它
def myview(request):
data = download_csv(ModelAdmin, request, Model.objects.all())
return HttpResponse (data, content_type='text/csv')
【讨论】:
这对我不起作用。 python3可以是原因吗?错误是“视图 app1.views.bulk_download 没有返回 HttpResponse 对象。它返回 None 。以上是关于如何将 Django 模型数据导出到 CSV 文件中的主要内容,如果未能解决你的问题,请参考以下文章
如何在django中上传csv文件的函数中使用外键连接模型?