首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Django中下载完整数据库的问题

在Django中下载完整数据库的问题
EN

Stack Overflow用户
提问于 2022-05-04 14:48:27
回答 1查看 65关注 0票数 0

首先,我使用export_import下载数据库,但一次只下载了一个表,但我需要将整个数据库导出到一个文件中,然后将其导入。现在我试着用我刚才看到的使用资源的另一种方式来修复它

如果你能帮我把它改成一个视图,而不是一个函数,我将不胜感激。

注意:谢谢你的帮助。

views.py

代码语言:javascript
复制
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

代码语言:javascript
复制
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        

这是用来调用下载数据库export_import_data_page.html的函数的html文件。

代码语言:javascript
复制
<!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

代码语言:javascript
复制
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 )
EN

回答 1

Stack Overflow用户

发布于 2022-05-04 14:56:22

试着把这个粘在你的views.py

代码语言:javascript
复制
import sys

from django.core.management import call_command


sysout = sys.stdout
sys.stdout = open('filename.json', 'w')
call_command('dumpdata')
sys.stdout = sysout

垃圾数据命令上也值得一读。

编辑:

您可能希望使用这些args进行dumpdata

代码语言:javascript
复制
call_command('dumpdata', natural_foreign=True, natural_primary=True, exclude=['contenttypes', 'auth'])
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72115023

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档