Django Database & Migrations
Relevant source files
The following files were used as context for generating this wiki page:
The Sunrise Communication backend utilizes Django's ORM and migration system to manage the enterprise hardware catalog. This system handles data persistence for products, brands, and categories, supporting both SQLite for local development and PostgreSQL for production environments.
The database architecture focuses on a hierarchical catalog structure, enabling bulk data imports via JSON and providing a structured schema for technical specifications.
Database Configuration
The project configures its database connection strings via environment variables. It defaults to a local SQLite database but switches to PostgreSQL if a DATABASE_URL starting with postgres is detected.
| Configuration Variable | Type | Default Value | Description |
|---|---|---|---|
DATABASE_URL | String | sqlite:///db.sqlite3 | Environment variable for DB connection string. |
ENGINE | String | django.db.backends.sqlite3 | Database engine driver. |
NAME | Path/String | BASE_DIR / 'db.sqlite3' | Name of the database file or DB instance. |
TIME_ZONE | String | Asia/Kolkata | Timezone for DateTime fields. |
Sources: sunrise-backend/config/settings.py:73-100
Product Catalog Schema
The core of the system is the products app, which defines several interconnected models. Every primary key in this schema uses its own UUIDField rather than standard integers to ensure unique identification across environments.
Entity Relationship Diagram
The following diagram represents the relationships between categories, sub-categories, brands, and products.
Model Definitions
The schema is divided into five primary entities that manage the hardware and telecommunication solutions catalog:
- Category: Defines high-level groupings like "Network Camera" or "EPABX & Intercom". It includes metadata for frontend rendering such as
icon_classandgradient_colors. - SubCategory: Provides a variant group within a category (e.g., "4 MP" under "Network Camera").
- Brand: Stores manufacturer details such as "CP Plus" or "Hikvision", including an optional logo image.
- Product: The central entity containing model numbers, descriptions, and a JSON field for
services_offered(e.g., ["Sales", "Installation"]). - ProductSpec: A key-value store for technical specifications (e.g., "Resolution": "4MP").
Sources: sunrise-backend/products/models.py:7-118
Initial Migration (0001_initial)
The 0001_initial.py migration file establishes the base state of the database. It creates the tables and sets up constraints, including unique_together for sub-categories within a category and PROTECT on-delete behaviors for foreign keys to prevent accidental deletion of referenced brands or categories.
migrations.CreateModel(
name='Product',
fields=[
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('brand', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='products', to='products.brand')),
('category', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='products', to='products.category')),
# ... other fields
],
)
Sources: sunrise-backend/products/migrations/0001_initial.py:59-86
Data Seeding and Import
The system supports bulk data entry through two primary methods: a management command and a custom Django Admin view. Both use a standard JSON format to create or update records.
Seed Management Command
The seed_products command reads from sunrise-angular/src/assets/data/products.json. It automates the population of the taxonomy and allows clearing the database before re-seeding using the --clear flag.
Admin JSON Upload
The ProductAdmin class extends the standard Django admin with a upload-json/ URL. This view parses uploaded .json files, maps category names to predefined CSS metadata (like flaticon-camera), and performs update_or_create operations on products.
Summary
The Django Database & Migrations setup for Sunrise Communication provides a robust foundation for a complex hardware catalog. By utilizing UUIDs for primary keys, protecting foreign key relationships, and implementing bulk JSON import logic, the architecture ensures data integrity and operational efficiency during both development and production phases.