Design a Django Model with Signals for Automated Workflows
Generate a Django model with status tracking, custom managers, and pre/post save signals for automated business workflows.
π The Prompt
Create a Django model called `[MODEL_NAME]` for a [APPLICATION_CONTEXT] application with associated signals that automate [WORKFLOW_DESCRIPTION].
**Model Definition:**
1. Define the model with these fields: [FIELD_DEFINITIONS] (include field types, constraints, help_text, and db_index where appropriate)
2. Add a `status` field using `TextChoices` with states: [STATUS_CHOICES] (e.g., draft, pending, approved, archived)
3. Include audit fields: `created_at`, `updated_at`, `created_by` (ForeignKey to User)
4. Implement these model methods:
- `clean()` for cross-field validation: [VALIDATION_RULES]
- `__str__()` returning a meaningful representation
- A custom manager method `[MANAGER_METHOD_NAME]` that filters by [FILTER_CRITERIA]
- A property `[COMPUTED_PROPERTY]` that calculates [CALCULATION_LOGIC]
**Signal Implementations:**
1. **`pre_save`**: Before saving, automatically [PRE_SAVE_ACTION] (e.g., generate slug, normalize data, validate state transitions). Prevent invalid status transitions by checking `old_value β new_value` against an allowed transitions map.
2. **`post_save`**: After creation (`created=True`), trigger [POST_CREATE_ACTION] (e.g., send notification, create related records, enqueue async task). After update, [POST_UPDATE_ACTION].
3. **`pre_delete`**: Before deletion, [PRE_DELETE_ACTION] (e.g., soft-delete instead, archive related data, check business rules).
**Deliverables:**
- `models.py` with the model, custom manager, and choices class
- `signals.py` with all signal handlers using the `@receiver` decorator
- `apps.py` with the `ready()` method importing signals
- A migration file or instructions for `makemigrations`
- 3 test cases in `tests/test_signals.py` verifying each signal fires correctly
Follow Django best practices: keep signals thin, use `update_fields` checks to avoid infinite loops, and document signal side effects.
π‘ Tips for Better Results
Define your status transition map explicitly in [STATUS_CHOICES] (e.g., 'draftβpendingβapproved') to get proper state machine validation in pre_save. Always request the update_fields infinite-loop guard β it's the most common Django signal bug. Keep signal handlers thin by delegating to service functions you can test independently.
π― Use Cases
Django developers use this when building models that need automated side effects on creation, update, or deletion β such as order processing, content approval, or audit logging workflows.