Skip to content

Commit dafda82

Browse files
authored
Merge pull request #66 from inertiajs/add-inertia-meta
Add InertiaMeta for automatic serialization
2 parents b254518 + 2b28d7d commit dafda82

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,30 @@ def inertia_share(get_response):
131131
return get_response(request)
132132
return middleware
133133
```
134+
### Prop Serialization
135+
136+
Unlike Rails and Laravel, Django does not handle converting objects to JSON by default so Inertia Django offers two different ways to handle prop serialization.
137+
138+
#### InertiaJsonEncoder
139+
140+
The default behavior is via the InertiaJsonEncoder. The InertiaJsonEncoder is a barebones implementation
141+
that extends the DjangoJSONEncoder with the ability to handle QuerySets and models. Models are JSON encoded
142+
via Django's `model_to_dict` method excluding the field `password`. This method has limitations though, as
143+
`model_to_dict` does not include fields where editable=False (such as automatic timestamps).
144+
145+
#### InertiaMeta
146+
147+
Starting in Inertia Django v1.2, Inertia Django supports an InertiaMeta nested class. Similar to Django Rest Framework's serializers, any class (not just models) can contain an InertiaMeta class which can specify how that class should be serialized to JSON. At this time, in only supports `fields`, but this may be extended in future versions.
148+
149+
```python
150+
class User(models.Model):
151+
name = models.CharField(max_length=255)
152+
password = models.CharField(max_length=255)
153+
created_at = models.DateField(auto_now_add=True)
154+
155+
class InertiaMeta:
156+
fields = ('name', 'created_at')
157+
```
134158

135159
### External Redirects
136160

inertia/tests/test_encoder.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from django.test import TestCase
55

6-
from inertia.tests.testapp.models import User
6+
from inertia.tests.testapp.models import Sport, User
77
from inertia.utils import InertiaJsonEncoder
88

99

@@ -31,6 +31,25 @@ def test_it_handles_models_with_dates_and_removes_passwords(self):
3131
self.encode(user),
3232
)
3333

34+
def test_it_handles_inertia_meta_fields(self):
35+
sport = Sport(
36+
id=3,
37+
name="Hockey",
38+
season="Winter",
39+
created_at=datetime(2022, 10, 31, 10, 13, 1),
40+
)
41+
42+
self.assertEqual(
43+
dumps(
44+
{
45+
"id": 3,
46+
"name": "Hockey",
47+
"created_at": "2022-10-31T10:13:01",
48+
}
49+
),
50+
self.encode(sport),
51+
)
52+
3453
def test_it_handles_querysets(self):
3554
User(
3655
name="Brandon",

inertia/tests/testapp/models.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,13 @@ class User(models.Model):
66
password = models.CharField(max_length=255)
77
birthdate = models.DateField()
88
registered_at = models.DateTimeField()
9+
created_at = models.DateField(auto_now_add=True)
10+
11+
12+
class Sport(models.Model):
13+
name = models.CharField(max_length=255)
14+
season = models.CharField(max_length=255)
15+
created_at = models.DateField(auto_now_add=True)
16+
17+
class InertiaMeta:
18+
fields = ("id", "name", "created_at")

inertia/utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ def model_to_dict(model):
1414

1515
class InertiaJsonEncoder(DjangoJSONEncoder):
1616
def default(self, value):
17+
if hasattr(value.__class__, "InertiaMeta"):
18+
return {
19+
field: getattr(value, field)
20+
for field in value.__class__.InertiaMeta.fields
21+
}
22+
1723
if isinstance(value, models.Model):
1824
return model_to_dict(value)
1925

0 commit comments

Comments
 (0)