Django

2010-03-24 6 views
5

में डेवेलो की नई बहु-डीबी कार्यक्षमता के साथ, मैं प्रबंधन कमांड बनाने पर काम करने की कोशिश कर रहा हूं, जो मुझे लाइव साइट से डेटा को डेवलपर को सिंक्रनाइज़ करने देता है विस्तारित परीक्षण के लिए मशीन। (वास्तविक डेटा होने के कारण, विशेष रूप से उपयोगकर्ता द्वारा दर्ज डेटा, मुझे इनपुट की एक विस्तृत श्रृंखला का परीक्षण करने की अनुमति देता है।)Django

अभी मुझे "अधिकतर" काम करने वाला आदेश मिला है। यह "सरल" मॉडल डेटा सिंक कर सकता है लेकिन मेरी समस्या यह है कि यह कईToMany फ़ील्ड को अनदेखा करता है जो मुझे ऐसा करने का कोई कारण नहीं दिखता है। किसी के पास कोई भी विचार है कि इसे कैसे ठीक किया जाए या इसे बेहतर तरीके से संभालना चाहते हैं? क्या मुझे पहली बार एक स्थिरता के लिए पहली क्वेरी निर्यात करनी चाहिए और फिर इसे फिर से आयात करना चाहिए?

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): 

     # Make sure we're running the command on a developer machine and that we've got the right settings 
     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 

     # Fetch all models for the given app 
     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) 

      # Query each model from the live site 
      qs = model.objects.all().using(LIVE_DATABASE_KEY) 

      # ...and save it to the local database 
      for record in qs: 
       try: 
        record.save(using='default') 
       except IntegrityError: 
        # Skip as the record probably already exists 
        pass 
+0

क्या यह विदेशी कुंजी के लिए काम करता है? –

+0

@Ofri - अगर उस पीके के साथ एक रिकॉर्ड मौजूद है तो यह काम करता है, लेकिन यह सुनिश्चित कर रहा है कि चीजें "क्रम में" बनाई गई हैं थोड़ा मुश्किल है –

+0

तो शायद यह एम 2 एम के लिए एक ही समस्या है? उन्हें अपने दोनों मॉडलों के बाद बनाया जाना है। –

उत्तर

0

यह आपके प्रश्न का बिल्कुल जवाब नहीं देता है, लेकिन केवल डीबी डंप और डीबी बहाल क्यों नहीं करते?

2

Django कमांड एक्सटेंशन Dumpscript बहुत मदद करनी चाहिए।

+0

ऐसा लगता है कि कुछ वादा है। मैं इसके साथ खेलूँगा। –