1

データベースとしてsqliteを使用するDjangoアプリがあります。そのsqliteファイルをダウンロードできるビューを作成するつもりはありません。

これは私がこれまでに得たものです:

def backup(request):
    """Return Sqlite3 db file, if Sqlite3 db is used."""
    import settings

    db_engine = settings.DATABASES['default']['ENGINE']
    #db_engine = 'JUST_FOR_TESTING'

    if db_engine == 'django.db.backends.sqlite3':
        db_path = settings.DATABASES['default']['NAME']

        response = HttpResponse(mimetype='application/x-sqlite3')
        response['Content-Disposition'] = 'attachment; filename=%s' % db_path
        response.write(db_path)

        return HttpResponse(response)
    else:
        return HttpResponse("settings.DATABASES['default']['ENGINE'] is %s,<br />\
                             only for 'django.db.backends.sqlite3' online backup is posible." % (db_engine))

私が見逃しているのは、そのファイルを添付ファイルとして追加する方法です。

また、特定のテーブルのみをダウンロードする方法はありますか?

4

1 に答える 1

7

ファイル パスを添付ファイルとして送信しようとしています。

関連するコードは

from django.conf import settings
from django.core.files import File
db_path = settings.DATABASES['default']['NAME']
dbfile = File(open(db_path, "rb"))
response = HttpResponse(dbfile, mimetype='application/x-sqlite3')
response['Content-Disposition'] = 'attachment; filename=%s' % db_path
response['Content-Length'] = dbfile.size

return response
于 2013-05-16T08:20:30.583 に答える