🎯 The Lightbulb Moment

Imagine you're working on a Django project, and you have a JSON field that feels like a wild west of data—unpredictable, unvalidated, and potentially chaotic. You've wished for a way to:

  • Define strict data structures
  • Validate incoming data effortlessly
  • Create meaningful relationships within your JSON fields
  • Maintain performance and integration with Django's ecosystem

Well, dream no more. 😈 Django Structured JSON Field is here to transform your JSON data handling!

🧩 The Genesis of the Project

Every great library is born from a real-world problem. In this case, the challenge was simple yet profound: how do we bring structure, validation, and intelligence to JSON fields in Django?

The Core Challenge

Django's JSONField is flexible, but flexibility can be a double-edged sword. Without proper validation, you risk:

  • Inconsistent data structures
  • Validation happening too late in the process
  • Complex and error-prone manual validation logic

Enter Pydantic: The Validation Superhero

The solution came from an unexpected ally—Pydantic, the validation powerhouse behind FastAPI. By leveraging Pydantic's robust model validation, we could bring order to the JSON chaos.

✨ Key Features That Make Django Structured JSON Field Shine

1. Pydantic-Powered Validation

from django.db import models
from structured.fields import StructuredJSONField
from structured.pydantic.models import BaseModel
from pydantic import Field

class Address(BaseModel):
    street: str = Field(..., description="Street address")
    city: str
    zip_code: str
    country: str = "United States"  # Default value demonstration

class User(models.Model):
    name = models.CharField(max_length=100)
    contact_info = StructuredJSONField(schema=Address)

In this example, the contact_info field is now a structured, validated JSON field. Try to save an incomplete or incorrect address, and Django Structured JSON Field will raise a validation error.

2. Django Model Relationships Reimagined

from django.db import models
from structured.fields import StructuredJSONField
from structured.pydantic.models import BaseModel

class Company(models.Model):
    name = models.CharField(max_length=100)

class Employee(BaseModel):
    name: str
    company: Company  # Direct relationship with Django model!

class TeamStructure(models.Model):
    team_details = StructuredJSONField(schema=Employee)

Yes, you read that right—you can now create relationships with Django models directly in your Pydantic models!

3. Performance-First Design

We didn't just stop at validation. A custom query optimization system ensures that these powerful features don't come at the cost of database performance. Complex relationships? Handled efficiently.

4. Seamless Framework Integration

  • Django Admin: Validation errors are displayed cleanly
  • Django Rest Framework: Serialization becomes a breeze
  • Custom Validation: Extend and customize as needed

🚀 Getting Started

Installation

pip install django-structured-json-field

Quick Setup

  1. Install the package
  2. Import StructuredJSONField and BaseModel
  3. Define your Pydantic models inheriting from BaseModel
  4. Use in Django models
  5. Enjoy validated, structured JSON fields!

🤔 Real-World Use Cases

  • E-commerce product specifications
  • User profile additional information
  • Configuration storage
  • Complex nested data structures
  • Page templates and content

🌟 Why Choose Django Structured JSON Field?

  • Clarity: Make your data intentions explicit
  • Validation: Catch errors early
  • Flexibility: Adapt to complex data needs without renouncing relationships
  • Performance: No compromise on speed
  • Integration: Works seamlessly with Django ecosystem

🤝 Community and Contribution

This is an open-source project, and we believe in community power!

  • Found a bug? Open an issue
  • Have a feature idea? Submit a pull request
  • Experiencing something unique? Share your use case!

📍 Learn More

Disclaimer: This library might just make your Django development too enjoyable. Use with caution! 😄🐍