在Django中下载完整数据库的问题
首先,我使用export_import下载数据库,但一次只下载了一个表,但是我需要将整个数据库导出到文件,然后将其导入。现在,我正在尝试以另一种方式修复它,
如果您可以帮助我将其更改为视图,而不是我将感激不尽的函数。
注意:感谢您的帮助。
views.py
from django.shortcuts import render
from django.http import HttpResponse
from .resources import CommentResource, CategoryResource
# Create your views here.
def export_data(request):
if request.method == 'POST':
# Get selected option from form
file_format = request.POST['file-format']
comment_resource = CommentResource()
dataset = comment_resource.export()
print(type(CommentResource()))
print(type(CategoryResource()))
if file_format == 'CSV':
response = HttpResponse(dataset.csv, content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="exported_data.csv"'
return response
elif file_format == 'JSON':
response = HttpResponse(dataset.json, content_type='application/json')
response['Content-Disposition'] = 'attachment; filename="exported_data.json"'
return response
elif file_format == 'XLS (Excel)':
response = HttpResponse(dataset.xls, content_type='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename="exported_data.xls"'
return response
return render(request, 'export_import_data_page.html')
resources.py
from import_export import resources
from label.models import Comment, Category
class CommentResource(resources.ModelResource):
class Meta:
model = Comment
class CategoryResource(resources.ModelResource):
class Meta:
model = Category
这是调用函数以下载数据库的HTML文件 export_import_data_page.html
<!DOCTYPE html>
<html>
<head>
<title>Título de mi página web</title>
</head>
<body>
<div class="card card-secondary">
<div class="card-header">
<h3 class="card-title">Export Comments</h3>
</div>
<div class="card-body">
<form role="form" method="POST" action="{% url 'label:export_data' %}" enctype="multipart/form-data">
{% csrf_token %}
<div class="form-group">
<label>Choose Format Type</label>
<select class="custom-select" name="file-format">
<option selected>Choose format...</option>
<option>CSV</option>
<option>JSON</option>
<option>XLS (Excel)</option>
</select>
</div> <br><br><br>
<button type="submit" class="btn btn-info btn-block">Export</button>
</form>
</div>
</div>
</body>
</html>
models.py
from django.db import models
class Category(models.Model):
title = models.CharField(max_length = 500, unique=True)
def __str__(self):
return "{0}".format(self.title)
class Comment(models.Model):
description = models.TextField()
category = models.ForeignKey(Category, on_delete= models.CASCADE )
First I used export_import to download the database but I only downloaded a single table at a time but I need to export the entire database to a file then import it back. Now I'm trying to fix it with another way I just saw using resources
If you could help me to change it to a view instead of a function I would be grateful.
Note: Thanks for the help.
views.py
from django.shortcuts import render
from django.http import HttpResponse
from .resources import CommentResource, CategoryResource
# Create your views here.
def export_data(request):
if request.method == 'POST':
# Get selected option from form
file_format = request.POST['file-format']
comment_resource = CommentResource()
dataset = comment_resource.export()
print(type(CommentResource()))
print(type(CategoryResource()))
if file_format == 'CSV':
response = HttpResponse(dataset.csv, content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="exported_data.csv"'
return response
elif file_format == 'JSON':
response = HttpResponse(dataset.json, content_type='application/json')
response['Content-Disposition'] = 'attachment; filename="exported_data.json"'
return response
elif file_format == 'XLS (Excel)':
response = HttpResponse(dataset.xls, content_type='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename="exported_data.xls"'
return response
return render(request, 'export_import_data_page.html')
resources.py
from import_export import resources
from label.models import Comment, Category
class CommentResource(resources.ModelResource):
class Meta:
model = Comment
class CategoryResource(resources.ModelResource):
class Meta:
model = Category
This is the html file to call the function to download the database
export_import_data_page.html
<!DOCTYPE html>
<html>
<head>
<title>Título de mi página web</title>
</head>
<body>
<div class="card card-secondary">
<div class="card-header">
<h3 class="card-title">Export Comments</h3>
</div>
<div class="card-body">
<form role="form" method="POST" action="{% url 'label:export_data' %}" enctype="multipart/form-data">
{% csrf_token %}
<div class="form-group">
<label>Choose Format Type</label>
<select class="custom-select" name="file-format">
<option selected>Choose format...</option>
<option>CSV</option>
<option>JSON</option>
<option>XLS (Excel)</option>
</select>
</div> <br><br><br>
<button type="submit" class="btn btn-info btn-block">Export</button>
</form>
</div>
</div>
</body>
</html>
models.py
from django.db import models
class Category(models.Model):
title = models.CharField(max_length = 500, unique=True)
def __str__(self):
return "{0}".format(self.title)
class Comment(models.Model):
description = models.TextField()
category = models.ForeignKey(Category, on_delete= models.CASCADE )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将其粘贴在您的
views.py
中也可能值得在 dumpdata 和 call_command
编辑:
您可能想使用这些args
dumpdata
:Try sticking this in your
views.py
Might also be worth reading up on dumpdata and call_command
Edit:
You probably want to
dumpdata
with these args: