Skip to main content

Django + msgspec = 🖤

Django JSONField with msgspec structs as a Schema.

Note: This library is a fork of django-pydantic-field that replaces Pydantic with msgspec for faster serialization and validation.

Why msgspec?

msgspec is a fast serialization library that offers:

  • High performance: 10-75x faster than other serialization libraries
  • Low memory usage: More memory-efficient than alternatives
  • Type validation: Built-in support for Python type hints
  • JSON Schema generation: Automatic schema generation for API documentation

Installation

Install the package with pip:

pip install django-msgspec-field

Usage

import msgspec
from datetime import date
from uuid import UUID

from django.db import models
from django_msgspec_field import SchemaField


class Foo(msgspec.Struct):
    count: int
    size: float = 1.0


class Bar(msgspec.Struct):
    slug: str = "foo_bar"


class MyModel(models.Model):
    # Infer schema from field annotation
    foo_field: Foo = SchemaField()

    # or explicitly pass schema to the field
    bar_list: list[Bar] = SchemaField(schema=list[Bar])

    # msgspec supports many types natively
    raw_date_map: dict[int, date] = SchemaField()
    raw_uids: set[UUID] = SchemaField()


# Usage
model = MyModel(
    foo_field={"count": "5"},
    bar_list=[{}],
    raw_date_map={1: "1970-01-01"},
    raw_uids={"17a25db0-27a4-11ed-904a-5ffb17f92734"}
)
model.save()

assert model.foo_field == Foo(count=5, size=1.0)
assert model.bar_list == [Bar(slug="foo_bar")]
assert model.raw_date_map == {1: date(1970, 1, 1)}
assert model.raw_uids == {UUID("17a25db0-27a4-11ed-904a-5ffb17f92734")}

The schema can be any type supported by msgspec, including:

  • msgspec.Struct classes
  • dataclasses
  • typing.TypedDict
  • Standard Python types (list, dict, set, etc.)
  • Unions, Optionals, and other generic types

Forward referencing annotations

It is also possible to use SchemaField with forward references and string literals:

class MyModel(models.Model):
    foo_field: "Foo" = SchemaField()
    bar_list: list["Bar"] = SchemaField(schema=typing.ForwardRef("list[Bar]"))


class Foo(msgspec.Struct):
    count: int
    size: float = 1.0


class Bar(msgspec.Struct):
    slug: str = "foo_bar"

The exact type resolution will be postponed until the initial access to the field, which usually happens on the first instantiation of the model.

The field performs checks against the passed schema during ./manage.py check command invocation:

  • msgspec.E001: The passed schema could not be resolved.
  • msgspec.E002: default= value could not be serialized to the schema.
  • msgspec.W003: The default value could not be reconstructed due to include/exclude configuration.

typing.Annotated support

SchemaField supports typing.Annotated[...] expressions for adding constraints:

import typing_extensions as te
import msgspec

class MyModel(models.Model):
    annotated_field: te.Annotated[int, msgspec.Meta(gt=0, title="Positive Integer")] = SchemaField()

Django Forms support

Create Django forms that validate against msgspec schemas:

from django import forms
from django_msgspec_field.forms import SchemaField


class Foo(msgspec.Struct):
    slug: str = "foo_bar"


class FooForm(forms.Form):
    field = SchemaField(Foo)


form = FooForm(data={"field": '{"slug": "asdf"}'})
assert form.is_valid()
assert form.cleaned_data["field"] == Foo(slug="asdf")

django_msgspec_field also supports auto-generated fields for ModelForm and modelform_factory:

class MyModelForm(forms.ModelForm):
    class Meta:
        model = MyModel
        fields = ["foo_field"]

form = MyModelForm(data={"foo_field": '{"count": 5}'})
assert form.is_valid()
assert form.cleaned_data["foo_field"] == Foo(count=5)

django-jsonform widgets

django-jsonform offers dynamic form construction based on JSONSchema. django_msgspec_field.forms.SchemaField works with its widgets:

from django_msgspec_field.forms import SchemaField
from django_jsonform.widgets import JSONFormWidget

class FooForm(forms.Form):
    field = SchemaField(Foo, widget=JSONFormWidget)

Override the default form widget for Django Admin:

from django.contrib import admin
from django_jsonform.widgets import JSONFormWidget
from django_msgspec_field.fields import MsgspecSchemaField

@admin.site.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
    formfield_overrides = {
        MsgspecSchemaField: {"widget": JSONFormWidget},
    }

Django REST Framework support

from rest_framework import generics, serializers
from django_msgspec_field.rest_framework import SchemaField, AutoSchema


class MyModelSerializer(serializers.ModelSerializer):
    foo_field = SchemaField(schema=Foo)

    class Meta:
        model = MyModel
        fields = '__all__'


class SampleView(generics.RetrieveAPIView):
    serializer_class = MyModelSerializer

    # optional support of OpenAPI schema generation for msgspec fields
    schema = AutoSchema()

Global approach with typed parser and renderer classes:

from rest_framework import views
from rest_framework.decorators import api_view, parser_classes, renderer_classes
from django_msgspec_field.rest_framework import SchemaRenderer, SchemaParser, AutoSchema


@api_view(["POST"])
@parser_classes([SchemaParser[Foo]])
@renderer_classes([SchemaRenderer[list[Foo]]])
def foo_view(request):
    assert isinstance(request.data, Foo)

    count = request.data.count + 1
    return Response([Foo(count=count)])


class FooClassBasedView(views.APIView):
    parser_classes = [SchemaParser[Foo]]
    renderer_classes = [SchemaRenderer[list[Foo]]]

    # optional support of OpenAPI schema generation
    schema = AutoSchema()

    def get(self, request, *args, **kwargs):
        assert isinstance(request.data, Foo)
        return Response([request.data])

    def put(self, request, *args, **kwargs):
        assert isinstance(request.data, Foo)
        count = request.data.count + 1
        return Response([request.data])

Global Settings

You can configure default enc_hook and dec_hook functions that will be used across all SchemaField instances when no custom hook is explicitly provided. This is useful for handling custom types globally without repeating the hook configuration on every field.

Add the DJANGO_MSGSPEC_FIELD setting to your Django settings.py:

# settings.py

def my_enc_hook(obj):
    """Custom encoder hook for unsupported types."""
    if isinstance(obj, MyCustomType):
        return obj.to_dict()
    raise NotImplementedError(f"Cannot encode {type(obj)}")


def my_dec_hook(type_, obj):
    """Custom decoder hook for unsupported types."""
    if type_ is MyCustomType:
        return MyCustomType.from_dict(obj)
    raise NotImplementedError(f"Cannot decode {type_}")


DJANGO_MSGSPEC_FIELD = {
    "ENC_HOOK": my_enc_hook,
    "DEC_HOOK": my_dec_hook,
}

You can also use dotted paths to reference functions defined elsewhere:

# settings.py
DJANGO_MSGSPEC_FIELD = {
    "ENC_HOOK": "myapp.hooks.my_enc_hook",
    "DEC_HOOK": "myapp.hooks.my_dec_hook",
}

Available Settings

Setting Type Default Description
ENC_HOOK Callable[[Any], Any] or str None Default encoder hook for serializing unsupported types. Called when msgspec encounters a type it cannot serialize natively.
DEC_HOOK Callable[[type, Any], Any] or str None Default decoder hook for deserializing custom types. Called when msgspec encounters a type it cannot deserialize natively.

Overriding Global Settings

You can always override the global settings on individual fields by passing enc_hook or dec_hook directly:

from django_msgspec_field import SchemaField


def custom_enc_hook(obj):
    # Field-specific encoding logic
    ...


class MyModel(models.Model):
    # Uses global enc_hook/dec_hook from settings
    data: MyStruct = SchemaField()

    # Uses custom enc_hook, overriding the global setting
    custom_data: MyStruct = SchemaField(enc_hook=custom_enc_hook)

Migrating from django-pydantic-field

If you're migrating from django-pydantic-field, here are the key changes:

  1. Replace imports:

    # Before (pydantic)
    from django_pydantic_field import SchemaField
    import pydantic
    
    class Foo(pydantic.BaseModel):
        count: int
    
    # After (msgspec)
    from django_msgspec_field import SchemaField
    import msgspec
    
    class Foo(msgspec.Struct):
        count: int
    
  2. Schema definitions: Replace pydantic.BaseModel with msgspec.Struct

  3. Config options: Remove pydantic.ConfigDict - msgspec uses different configuration methods

  4. Validators: Replace Pydantic validators with msgspec constraints using msgspec.Meta

Contributing

To get django-msgspec-field up and running in development mode:

  1. Clone this repo: git clone https://github.com/quertenmont/django-msgspec-field.git
  2. Install uv: curl -LsSf https://astral.sh/uv/install.sh | sh
  3. Install dependencies: uv sync --all-extras
  4. Setup pre-commit: pre-commit install
  5. Run tests: uv run pytest
  6. Run linting: uv run ruff check .
  7. Run type checking: uv run mypy django_msgspec_field

License

Released under MIT License.


Supporting

  • :star: Star this project on GitHub
  • :octocat: Follow me on GitHub
  • :blue_heart: Follow me on Twitter
  • :moneybag: Sponsor me on Github

You can also support me via:


Acknowledgement

Release files for django-msgspec-field 0.1.14

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-msgspec-field 0.1.14
File Size Uploaded
django_msgspec_field-0.1.14.tar.gz 22.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-msgspec-field 0.1.14
File Interpreter ABI Platform
django_msgspec_field-0.1.14-py3-none-any.whl Python 3 none any Details

Total release size: 53.2 kB

Release files / django_msgspec_field-0.1.14.tar.gz

Download URL django_msgspec_field-0.1.14.tar.gz
Size 22.2 kB
Tags Source
SHA-256 checksum
How to use checksums
6920112ab38d0d1840ac0c5cabe467c1a9f725e5641f59cda68fe6c38992598e
BLAKE2b-256 checksum
How to use checksums
3811c7ea81d5d6dfba1d09822c2f3cff4bd7bfbb3525e300997d2302f22682aa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 26, 2026.

Transparency log

Release files / django_msgspec_field-0.1.14-py3-none-any.whl

Download URL django_msgspec_field-0.1.14-py3-none-any.whl
Size 31.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
820d4f8a1bbf6eb4798889d56618f76d7c32d546f9afd74e2da112110e314c09
BLAKE2b-256 checksum
How to use checksums
1e2b0bc0dfb86b931f0faa59b009083f40789d0fe350c3981b4f3bc8319f8049
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 26, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.14 This release

2 release files

0.1.11

2 release files

0.1.10

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page