Skip to main content

Django app for database data synchronization.

Project description

Aim & purpose

This app is for synchronization of django objects between databases.

It logs information about objects’ manipulations (additions, changes, deletions). When synchronization is launched, all objects logged from the last checkpoint are synced to another database.

Important note: This app doesn’t log detailed information about changes (e.g. which fields were updated), just that such manipulation occured. When the synchronization is performed, the objects are synced with their newest, actual values. (however, you can specify some fields to be skipped during synchronization, see below).

Example 1

Consider scenario:

  • there is one production project deployed on the web
  • and the same project is deployed on some office computer in case of main server failure

Assuming that the local database is regularly synced (eg. once a day the main database is exported and imported into the local system), in case of a long main server downtime the staff may use the local project (inserting objects etc.).

After the server is up again, the local changes (from the point of the last checkpoint) can be painlessly synchronized to the remote server.

Example 2

You can also synchronize databases both ways, not only in the slave-master model like in the previous example.

However, it is probably better (if possible) to have a common database rather than to have one for every project deployment and to perform synchronization between them.


  1. Install app (note: django-dbsettings is required and please view its install notes, such as cache backend important remarks):

    $ pip install django-synchro

    or download it manually and put in python path.

  2. Configure DATABASES.

  3. Add synchro to INSTALLED_APPS.

  4. Specify in your what is remote database name and which models should be watched and synchronized:

    SYNCHRO_REMOTE = 'remote'
        'my_first_app', # all models from my_first_app
        ('my_second_app', 'model1', 'model2'), # only listed models (letter case doesn't matter)
        'my_third_app', # all models again

Later, REMOTE will mean remote database.



Just invoke synchronize management command:

$ ./ synchronize

Admin synchro view

In order to allow performing synchronization without shell access, you can use special admin view.

Include in your urls:

url(r'^synchro/', include('synchro.urls', 'synchro', 'synchro')),

Then the view will be available at reversed url: synchro:synchro.


Generally, SYNCHRO_REMOTE setting can behave in 3 different ways:

  1. The most naturally: it holds name of REMOTE database. When synchronize is called, sychro will sync objects from LOCAL database to REMOTE one.
  2. When SYNCHRO_REMOTE is None: it means that no REMOTE is needed as synchro will only store logs (see below). It’s useful on REMOTE itself.
  3. When SYNCHRO_REMOTE is not specified at all, it behaves just like above (as if it was None), but will show a RuntimeWarning.

Remarks and features

QuerySet update issue

Django-synchro logs information about objects modifications and later use it when asked for synchronization.

The logging take place using the post_save and post_delete signal handlers.

That means that actions which don’t emmit those signals (like objects.update method) would result in no log stored, hence no synchronization of actions’ objects.

So, please remind: objects modified via objects.update won’t be synchronized unless some special code is prepared (eg. calling save on all updated objects or manually invoking post_save signal).

Natural keys

For efficient objects finding, it is highly suggested to provide natural_key object method and get_by_natural_key manager method. This will allow easy finding whether the synchronized object exists in REMOTE and to prevent duplicating.

Although adding natural_key to model definition is relatively quick, extending a manager may require extra work in cases when the default manager is used:

class MyManager(models.Manager):
    def get_by_natural_key(self, code, day):
        return self.get(code=code, day=day)

class MyModel(models.Model):
    objects = MyManager()
    def natural_key(self):
        return self.code,

To minimalize the effort of implementing a custom manager, a shortcut is provided:

from synchro import NaturalManager

class MyModel(models.Model):
    objects = NaturalManager('code', 'day')
    def natural_key(self):
        return self.code,

Or even easier (effect is exactly the same):

from synchro import NaturalKeyModel

class MyModel(NaturalKeyModel):
    _natural_key = ('code', 'day')

NaturalManager extends the built-in Manager by default; you can change its superclass using manager keyword:

from synchro import NaturalManager

class MyVeryCustomManager(models.Manager):
    ... # some mumbo-jumbo magic

class MyModel(models.Model):
    objects = NaturalManager('code', 'day', manager=MyVeryCustomManager)
    def natural_key(self):
        return self.code,

When using NaturalKeyModel, NaturalManager will extend the defined (objects) manager:

from synchro import NaturalKeyModel

class MyVeryCustomManager(models.Manager):
    ... # some mumbo-jumbo magic

class MyModel(NaturalKeyModel):
    _natural_key = ('code', 'day')
    objects = MyVeryCustomManager()

Side note: in fact invoking NaturalManager creates a new class being NaturalManager’s subclass.

The purpose of a natural key is to uniquely distinguish among model instances; however, there are situations where it is impossible. You can choose such fields that will cause get_by_natural_key to find more than one object. In such a situation, it will raise MultipleObjectsReturned exception and the synchronization will fail.

But you can tell NaturalManager that you are aware of such a situation and that it should just take the first object found:

class Person(models.Model):
    # combination of person name and city is not unique
    objects = NaturalManager('first_name', 'last_name', 'city', allow_many=True)
    def natural_key(self):
        return self.first_name, self.last_name,

Or with NaturalKeyModel:

class Person(NaturalKeyModel):
    # combination of person name and city is not unique
    _natural_key = ('first_name', 'last_name', 'city')
    _natural_manager_kwargs = {'allow_many': True}  # I know, it looks quite ugly

Don’t use allow_many unless you are completely sure what you are doing and what you want to achieve.

Side note: if natural_key consist of only one field, be sure to return a tuple anyway:

class MyModel(models.Model):
    objects = NaturalManager('code')
    def natural_key(self):
        return self.code,  # comma makes it tuple

Or to assign tuple in NaturalKeyModel:

_natural_key = ('code',)

Previously, there were natural_manager function that was used instead of NaturalManager - however, it’s deprecated.

Skipping fields

If your model has some fields that should not be synchronized, like computed fields (eg. field with payment balances, which is updated on every order save - in order.post_save signal), you can exclude them from synchronization:

class MyModel(models.Model):
    SYNCHRO_SKIP = ('balance',)

When a new object is synchronized, all its skipped fields will be reset to default values on REMOTE. Of course, the LOCAL object will stay untouched.

Temporary logging disabling

If you don’t want to log some actions:

from synchro import DisableSynchroLog

with DisableSynchroLog(): = foo

Or, in a less robust way, with a decorator:

from synchro import disable_synchro_log

def foo(mymodel): = foo


That’s a harder part.

If your signal handlers modify other objects, such an action will be probably reproduced twice:

  • first, when the model will be updated on REMOTE, then normal REMOTE signal handler will launch
  • second time, because the original signal handler’s action was logged, the whole modified object will be synchronized; this is probably undesirable.

Consider a bad scenario:

  1. Initially databases are synced. There is an object A in each of the databases. and values are both 1.
  2. On REMOTE, we change to 42 and save.
  3. On LOCAL, we save object X. In some X signal handler, is incremented.
  4. We perform synchronization:
    1. X is synced.
    2. X signal handler is invoked on REMOTE, resulting in REMOTE’s incrementation. So far so good. REMOTE’s == 2 and == 42, just like it should.
    3. Because A change (during step 3) was logged, A is synced. Not good - REMOTE value of will be overwritten with 1 (because LOCAL version is considered newer, as it was saved later).

It happened because the signal handler actions were logged.

To prevent this from happening, wrap handler with DisableSynchroLog:

@receiver(models.signals.post_delete, sender=Parcel)
def update_agent_balance_delete(sender, instance, *args, **kwargs):
    with DisableSynchroLog():
        instance.agent.balance -= float(instance.payment_left))

Or with the decorator:

@receiver(models.signals.post_delete, sender=Parcel)
def update_agent_balance_delete(sender, instance, *args, **kwargs):
    instance.agent.balance -= float(instance.payment_left))

If using the decorator, be sure to place it after connecting to the signal, not before - otherwise it won’t work.

Update issue again

One can benefit from the fact that objects.update is not logged and use it in signal handlers instead of DisableSynchroLog.

Signal handlers for multi-db

Just a reminder note.

When a synchronization is performed, signal handlers are invoked for created/updated/deleted REMOTE objects. And those signals are of course handled on the LOCAL machine.

That means: signal handlers (and probably other part of project code) must be ready to handle both LOCAL and REMOTE objects. It must use using(...) clause or db_manager(...) to ensure that the proper database is used:

def reset_specials(sender, instance, *args, **kwargs):

Plain objects, without db_manager or using, always use the default database (which means LOCAL).

But that is normal in multi-db projects.

Synchro on REMOTE and time comparing

If you wish only to synchronize one-way (always from LOCAL to REMOTE), you may be tempted not to include synchro in REMOTE INSTALLED_APPS.

Yes, you can do that and you will save some resources - logs won’t be stored.

But keeping synchro active on REMOTE is a better idea. It will pay at synchonization: the synchro will look at logs and determine which object is newer. If the LOCAL one is older, it won’t be synced.

You probably should set SYNCHRO_REMOTE = None on REMOTE if no synchronizations will be performed there (alternatively, you can add some dummy sqlite database to DATABASES).


If you wish to reset sychronization status (that is - delete logs and set checkpoint):

from synchro import reset_synchro


Or raw way of manually changing synchro checkpoint:

from synchro.models import options

options.last_check =  # or any time you wish


0.4.1 (23/09/2012)
  • Fixed symmetrical m2m synchronization
  • Added 1 test regarding the issue above
0.4 (16/09/2012)
  • Deprecation: natural_manager function is deprecated. Use NaturalManager instead
  • Refactored NaturalManager class so that it plays well with models involved in m2m relations
  • Refactored NaturalManager class so that natural_manager function is unnecessary
  • Added NaturalKeyModel base class
  • Fixed bug with m2m user-defined intermediary table synchronization
  • Fixed bugs with m2m changes synchronization
  • Added 3 tests regarding m2m aspects
0.3.1 (12/09/2012)
  • SYNCHRO_REMOTE setting is not required anymore. Its lack will only block synchronize command
  • Added 2 tests regarding the change above
  • Updated README
0.3 (04/09/2012)
  • Backward incompatible: Changed Reference fields type from Integer to Char in order to store non-numeric keys
  • Included 24 tests
  • Refactored NaturalManager class so that it is accessible and importable
  • Exception is raised if class passed to natural_manager is not Manager subclass
  • Switched to dbsettings-bundled DateTimeValue
  • Updated README
0.2 (10/06/2012)
Initial PyPI release
Local development

Author:Jacek Tomaszewski
Thanks:to my fiancee for text correction

Project details

Release history Release notifications

History Node


History Node


History Node


History Node


History Node


History Node


This version
History Node


History Node


History Node


History Node


History Node


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
django-synchro-0.4.1.tar.gz (24.8 kB) Copy SHA256 hash SHA256 Source None Sep 23, 2012

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page