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.

πŸ”— Related Prompts

πŸ’» Coding beginner

Explain Code Like Im a Beginner

Get any code explained in plain English with line-by-line breakdowns, analogies, and learning suggestions.

πŸ’» Coding beginner

Debug My Code and Explain the Fix

Get your code debugged with clear explanations of what went wrong and why, plus the corrected version.

πŸ’» Coding intermediate

Write Unit Tests for My Code

Generate thorough unit tests covering edge cases, error handling, and both positive and negative scenarios.

πŸ’» Coding intermediate

Convert Code Between Languages

Convert code between any programming languages while maintaining idiomatic patterns and best practices.

πŸ’» Coding intermediate

Write a REST API Endpoint

Generate production-ready REST API endpoints with validation, error handling, and documentation.

πŸ’» Coding advanced

Create a GitHub Actions CI/CD Workflow for Automated Testing and Deployment

Generate a complete GitHub Actions CI/CD workflow with build, test, deploy, and notification jobs for your project.