With the new Django functionality with several db in the development version, I tried to work on creating a management team that allows me to synchronize data from a live site up to the developer's machine for advanced testing. (Having actual data, in particular user input, allows me to test a wider range of inputs.)
Now I have a βcoreβ work team. It can synchronize βsimpleβ model data, but the problem I am facing is that it ignores ManyToMany fields , which I see for no reason. Anyone have any ideas on how to fix this or is it better to want to deal with it? Should I first export this first request to the device and then re-import it?
from django.core.management.base import LabelCommand
from django.db.utils import IntegrityError
from django.db import models
from django.conf import settings
LIVE_DATABASE_KEY = 'live'
class Command(LabelCommand):
help = ("Synchronizes the data between the local machine and the live server")
args = "APP_NAME"
label = 'application name'
requires_model_validation = False
can_import_settings = True
def handle_label(self, label, **options):
db_settings = getattr(settings, 'DATABASES', {})
if not LIVE_DATABASE_KEY in db_settings:
print 'Could not find "%s" in database settings.' % LIVE_DATABASE_KEY
return
if db_settings.get('default') == db_settings.get(LIVE_DATABASE_KEY):
print 'Data cannot synchronize with self. This command must be run on a non-production server.'
return
try:
app = models.get_app(label)
app_models = models.get_models(app)
except:
print "The app '%s' could not be found or models could not be loaded for it." % label
for model in app_models:
print 'Syncing %s.%s ...' % (model._meta.app_label, model._meta.object_name)
qs = model.objects.all().using(LIVE_DATABASE_KEY)
for record in qs:
try:
record.save(using='default')
except IntegrityError:
pass
source
share