I'm going to use django-dbbackup in my current application. My task is to take backup of my latest sqlite3 database with custom_name.db when 'Backup Database' button is pressed from the UI and to restore a backup from a list of existing backups when 'Restore this backup' is pressed.
In django-dbbackup there are two management commands, dbbackup and dbrestore which are used as
dbbackup [-s <servername>] [-d <database>] [--clean] [--compress] [--encrypt]
and
dbrestore [-d <database>] [-s <servername>] [-f <localfile>]
Now my question is, if I have the original db name original_db.db and I want to backup this db renaming as db_current_data_time.db, what should be the views.py methods?
You can call commands run by manage.py
using call_command
from django.core import management
management.call_command('your_command', your_options)
So in your respective views of backup and restore you can call your commands.
While you can use call_command
to call a management command, it is really not ideal. Management commands should be run interactively by a human from the shell, not called from a view.
If you want to offer both a management command and a web operation, move the guts of your management command into a separate function (I recommend myapp/operations/foo
). Then refactor your management command to utilize this independent function. Once that's working, refactor your view to call the same operation (function), passing in the same arguments.
This will allow for optimal code sharing between the management command and the view, and will make writing tests for your core logic more sane.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With