Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is my solution, which fits all of my requirements:</p> <ol> <li>Implement <a href="https://docs.djangoproject.com/en/dev/topics/serialization/#natural-keys" rel="nofollow">natural keys</a> and unique constraints on all models <ul> <li>Allows for a unique way to refer to each object without using primary key IDs</li> </ul></li> <li>Sublcass each model from <a href="https://github.com/django-extensions/django-extensions/blob/master/django_extensions/db/models.py" rel="nofollow">TimeStampedModel</a> in django-extensions <ul> <li>Adds automatically updated <code>created</code> and <code>modified</code> fields</li> </ul></li> <li><p>Create a Django management command for exporting, which filters a subset of data and serializes it with natural keys</p> <pre><code>baz = Baz.objects.filter(foo=bar) yaz = Yaz.objects.filter(foo=bar) objects = [baz, yaz] flat_objects = list(itertools.chain.from_iterable(objects)) data = serializers.serialize("json", flat_objects, indent=3, use_natural_keys=True) print(data) </code></pre></li> <li><p>Create a Django management command for importing, which reads in the serialized file and iterates through the objects as follows: </p> <ul> <li>If the object does not exist in the database (by natural key), create it </li> <li>If the object exists, check the <code>modified</code> timestamps</li> <li>If the imported object is newer, update the fields</li> <li>If the imported object is older, do not update (but print a warning)</li> </ul></li> </ol> <p>Code sample:</p> <pre><code># Open the file with open(args[0]) as data_file: json_str = data_file.read() # Deserialize and iterate for obj in serializers.deserialize("json", json_str, indent=3, use_natural_keys=True): # Get model info model_class = obj.object.__class__ natural_key = obj.object.natural_key() manager = model_class._default_manager # Delete PK value obj.object.pk = None try: # Get the existing object existing_obj = model_class.objects.get_by_natural_key(*natural_key) # Check the timestamps date_existing = existing_obj.modified date_imported = obj.object.modified if date_imported &gt; date_existing: # Update fields for field in obj.object._meta.fields: if field.editable and not field.primary_key: imported_val = getattr(obj.object, field.name) existing_val = getattr(existing_obj, field.name) if existing_val != imported_val: setattr(existing_obj, field.name, imported_val) except ObjectDoesNotExist: obj.save() </code></pre> <p>The workflow for this is to first call <code>python manage.py exportTool &gt; data.json</code>, then on another django instance (or the same), call <code>python manage.py importTool data.json</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload