Skip to content

Model Signals in ORMBridge ​

Overview ​

ORMBridge keeps your frontend and backend data in sync by using signals. This document explains how these signals work and best practices for using them.

How Signals Work in ORMBridge ​

Signals in ORMBridge serve two critical purposes:

  1. Data Synchronization: Keeping frontend data in sync with backend changes
  2. Cache Invalidation: Ensuring cached data is refreshed when models are modified

When you register a model with ORMBridge:

  1. Standard Operations: Creating, updating, or deleting individual instances through Django triggers the standard Django signals (post_save and post_delete).

  2. Bulk Operations: When performing bulk operations from the frontend, ORMBridge uses its own special signals (not Django signals) to ensure cache invalidation works properly.

python
# Triggers Django post_save signal
instance = MyModel.objects.create(name="Example")

# Triggers Django post_save signal
instance.name = "Updated"
instance.save()

# Triggers Django post_delete signal
instance.delete()

Important Considerations ​

Bypassing ORMBridge ​

If you perform bulk operations directly with Django's ORM (bypassing ORMBridge), no signals will be triggered:

python
# No signals triggered
MyModel.objects.filter(category="old").update(category="new")

# No signals triggered
MyModel.objects.filter(is_archived=True).delete()

This can cause your frontend to display outdated information.

Best Practices ​

To keep your data consistent:

  1. Use the ORMBridge client for all operations when possible.

  2. If you must use direct Django ORM (for performance with large datasets), manually trigger signals:

python
# For bulk updates:
affected_records = MyModel.objects.filter(category="old")
affected_ids = list(affected_records.values_list('id', flat=True))
affected_records.update(category="new")

# Manually trigger signals
from django.db.models.signals import post_save
for instance in MyModel.objects.filter(id__in=affected_ids):
    post_save.send(sender=MyModel, instance=instance, created=False)
  1. For large datasets, consider:
    • Working in smaller batches
    • Using background tasks
    • Temporarily disabling signals and manually invalidating caches afterward

Summary ​

  • ORMBridge uses Django signals for standard operations
  • ORMBridge uses its own special signals for frontend bulk operations
  • Both types of signals ensure proper data synchronization and cache invalidation
  • Operations outside ORMBridge won't trigger any signals
  • Always use ORMBridge when possible, or manually trigger signals when bypassing it

Not MIT Licensed - See Licensing section for details