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
- 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 = ...
Optional: Setup for automatic syncing via Webhooks
- 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
Usage
- 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]
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
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
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
..._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 the
dato_sync.pyfile. - For one-to-many relationships use absolute paths to access the parent's id
- ⚠️ 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
- 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file django_dato_sync-0.0.13.tar.gz.
File metadata
- Download URL: django_dato_sync-0.0.13.tar.gz
- Upload date:
- Size: 15.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b2e7218193fa5bb266721080735fd29719ac03da4d00de2d4c6b17683447fc25
|
|
| MD5 |
812cbeb28dcfbe3f4f8f173596bf54e2
|
|
| BLAKE2b-256 |
73391638e314a765d00bc6372106d3d7274698c5567f2b74a50cd65f8e59b1b5
|