Skip to main content

A sync utility for storing DatoCMS objects in django.

Project description

django-dato-sync

django-dato-sync enables you to easily sync Dato records into your django database. Features include:

  • Delta sync
  • Automatic sync via Dato webhooks
  • Localization support with django-modeltranslation
  • Configuration of which fields to sync
  • Collecting information of multiple Dato records into a single django object

Installation

  1. Install the pip package:
pipenv install django-dato-sync
  1. Add to your installed apps:
INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.staticfiles",
    ...
    "dato_sync",
]
  1. Setup at least the following settings:
DATOCMS_API_TOKEN: str = ...
DATOCMS_API_URL: str = ...
DATOCMS_ENVIRONMENT: str = ...

Optional: Setup for automatic syncing via Webhooks

  1. Add the following to your urls.py:
urlpatterns = [
    ...
    path("", include("dato_sync.urls"))
]
  1. Setup the authentication header Dato will use in your settings.py
DATO_SYNC_WEBHOOK_EXPECTED_AUTH: str = "Basic ..."
  1. In Dato navigate to Project Setting > Automations > Webhooks and click "Add a new webhook"
  2. Configure the webhook to trigger on "Publish", "Unpublish", and "Delete" for any records you want to sync to django
  3. Specify the URL as https://<your-django-server-address>/dato-sync/sync/
  4. Configure the "HTTP basic auth" to match the header you configured in step 2

Usage

  1. Create your local django model but make sure it inherits from DatoModel (it will automatically have fields for the dato_identifier (pk), created and modified dates (corresponding to changes made in Dato) and a deleted field indicating a soft delete)
class MyModel(DatoModel):
    name = models.TextField()
    order = models.IntegerField()
    note = models.TextField()
  1. Create a dato_sync.py file in your app:
@fetch_from_dato(MyModel)
class MyModelSyncOptions(SyncOptions):
    dato_model_path = "my_model"
    field_mappings = [
        "order" |position_in_parent,
        "name",
    ]
  1. To sync either have Dato call the webhook (see above) or use
python manage.py sync_dato [--force-full-sync]

Configuration Options

dato_model_path

Configure the dato entity your model corresponds to. If you are mapping a Dato model directly this is just its model id. If you're using blocks you can chain them like my_model.parent_block.child_block.

⚠️ Make sure that all blocks have the same schema. Fields that can contain different types of blocks are currently not supported.

field_mappings

Here you specify which fields should be synced with dato. If you leave out fields (like the notes field in the example above they can be edited locally).

In the simplest case when the name of your field in django corresponds to the api name in Dato, you can simply add it to the field mappings as we did with "name". For more complicated scenarios you can write:

field_mappings = [
    "name" |from_dato_path("my_model.title", localized=True, absolute=True),
]

This allows you to

  • specify a different name / path to take the value from
  • localized allows you to fetch localizations from Dato and store them using django-modeltranslation
  • absolute allows you to access properties of the parent entities by specifying the field to take the value from starting from the top of the Dato query rather than the path specified in dato_model_path

Additonally the following are also available:

  • |position_in_parent to obtain the position of the item in its parent
  • |flattened_position to obtain a global order by flattening the list across all paths

ArrayFields

Postgres ArrayFields are supported ⚠️ so long as there is no nesting on either the Dato or django side ⚠️. Simply specify the path like:

"tags" |from_dato_path("tags.name")

and django-dato-sync will automatically collect the names of all tags into an array.

Tips and Tricks

  • Foreign key relationships are not supported directly, but you can use django's ..._id field to set the id of another Dato object
    • ⚠️ Make sure to sync the related objects first to avoid foreign key constraint violations. Sync operations are executed in the same order they appear in the dato_sync.py file.
    • For one-to-many relationships use absolute paths to access the parent's id
  • You can create multiple sync jobs for the same django model to collect all instances of a block across multiple models into one django table

Project details


Download files

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

Source Distribution

django_dato_sync-0.0.1.tar.gz (9.4 kB view details)

Uploaded Source

File details

Details for the file django_dato_sync-0.0.1.tar.gz.

File metadata

  • Download URL: django_dato_sync-0.0.1.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for django_dato_sync-0.0.1.tar.gz
Algorithm Hash digest
SHA256 a637c22fc56aea9f2a91e030113e1c91b568aec5df3ace17ad8ffb25bedfb298
MD5 eb1ca889d102673ef8e56c6bf3beedc5
BLAKE2b-256 d3c5d18dc00c15f30d9ea6a8aeffecf19d24f9572578de424b89ada0e88229a0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page