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
- Install the pip package:
pipenv install django-dato-sync- Add to your installed apps:
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.staticfiles",
...
"dato_sync",
]- Setup at least the following settings:
DATOCMS_API_TOKEN: str = ...
DATOCMS_API_URL: str = ...
DATOCMS_ENVIRONMENT: str = ...- Add the following to your
urls.py:
urlpatterns = [
...
path("", include("dato_sync.urls"))
]- Use
python manage.py gen_auth_headerto generate the auth setup. AddDATO_SYNC_WEBHOOK_EXPECTED_AUTH: strto yoursettings.pyand set up the username and password in Dato. - In Dato navigate to Project Setting > Automations > Webhooks and click "Add a new webhook"
- Configure the webhook to trigger on "Publish", "Unpublish", and "Delete" for any records you want to sync to django
- Specify the URL as
https://<your-django-server-address>/dato-sync/sync/ - Configure the "HTTP basic auth" to match the header you configured in step 2
- Create your local django model but make sure it inherits from
DatoModel(it will automatically have fields for thedato_identifier(pk),createdandmodifieddates (corresponding to changes made in Dato) and adeletedfield indicating a soft delete)
class MyModel(DatoModel):
name = models.TextField()
order = models.IntegerField()
note = models.TextField()- Create a
dato_sync.pyfile in your app:
@fetch_from_dato(MyModel)
class MyModelSyncOptions(SyncOptions):
dato_model_path = "my_model"
field_mappings = [
"order" |position_in_parent,
"name",
]- To sync either have Dato call the webhook (see above) or use
python manage.py sync_dato [--force-full-sync]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.
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
localizedallows you to fetch localizations from Dato and store them either- using django-modeltranslation
- by manually defining fields with the
_<language_code>suffix (e.g.foo_de,foo_fr)
absoluteallows 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 indato_model_path
Additonally the following are also available:
|position_in_parentto obtain the position of the item in its parent|flattened_positionto obtain a global order by flattening the list across all paths
Postgres ArrayFields are supported
"tags" |from_dato_path("tags.name")and django-dato-sync will automatically collect the names of all tags into an array.
- Foreign key relationships are not supported directly, but you can use django's
..._idfield 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 thedato_sync.pyfile.- 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