Skip to content

Commit 20a7a98

Browse files
committed
add more docs, add final tests for now, and improve imports
1 parent efde093 commit 20a7a98

File tree

5 files changed

+64
-6
lines changed

5 files changed

+64
-6
lines changed

README.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ You can also check out the official Inertia docs at https://inertiajs.com/.
5252
Render Inertia responses is simple, you can either use the provided inertia render function or, for the most common use case, the inertia decorator. The render function accepts four arguments, the first is your request object. The second is the name of the component you want to render from within your pages directory (without extension). The third argument is a dict of `props` that should be provided to your components. The final argument is `view_data`, for any variables you want to provide to your template, but this is much less common.
5353

5454
```python
55-
from inertia.http import render
55+
from inertia import render
5656
from .models import Event
5757

5858
def index(request):
@@ -64,7 +64,7 @@ def index(request):
6464
Or use the simpler decorator for the most common use cases
6565

6666
```python
67-
from inertia.http import inertia
67+
from inertia import inertia
6868
from .models import Event
6969

7070
@inertia('Event/Index')
@@ -79,7 +79,7 @@ def index(request):
7979
If you have data that you want to be provided as a prop to every component (a common use-case is information about the authenticated user) you can use the `share` method. A common place to put this would be in some custom middleware.
8080

8181
```python
82-
from inertia.share import share
82+
from inertia import share
8383
from django.conf import settings
8484
from .models import User
8585

@@ -95,6 +95,21 @@ def inertia_share(get_response):
9595
return middleware
9696
```
9797

98+
### Lazy Props
99+
On the front end, Inertia supports the concept of "partial reloads" where only the props requested
100+
are returned by the server. Sometimes, you may want to use this flow to avoid processing a particularly slow prop on the intial load. In this case, you can use `Lazy props`. Lazy props aren't evaluated unless they're specifically requested by name in a partial reload.
101+
102+
```python
103+
from inertia import lazy, inertia
104+
105+
@inertia('ExampleComponent')
106+
def example(request):
107+
return {
108+
'name': lambda: 'Brandon', # this will be rendered on the first load as usual
109+
'data': lazy(lambda: some_long_calculation()), # this will only be run when specifically requested by partial props and WILL NOT be included on the initial load
110+
}
111+
```
112+
98113
### Json Encoding
99114

100115
Inertia Django ships with a custom JsonEncoder at `inertia.utils.InertiaJsonEncoder` that extends Django's

inertia/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .http import inertia, render
2+
from .utils import lazy
3+
from .share import share

inertia/tests/test_encoder.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from inertia.tests.testapp.models import User
2+
from django.test import TestCase
3+
from inertia.utils import InertiaJsonEncoder
4+
from json import dumps
5+
from datetime import date, datetime
6+
7+
class InertiaJsonEncoderTestCase(TestCase):
8+
def setUp(self):
9+
self.encode = lambda obj: dumps(obj, cls=InertiaJsonEncoder)
10+
11+
def test_it_handles_models_with_dates_and_removes_passwords(self):
12+
user = User(
13+
name='Brandon',
14+
password='something-top-secret',
15+
birthdate=date(1987, 2, 15),
16+
registered_at=datetime(2022, 10, 31, 10, 13, 1),
17+
)
18+
19+
self.assertEqual(
20+
dumps({'id': None, 'name': 'Brandon', 'birthdate': '1987-02-15', 'registered_at': '2022-10-31T10:13:01'}),
21+
self.encode(user)
22+
)
23+
24+
def test_it_handles_querysets(self):
25+
User(
26+
name='Brandon',
27+
password='something-top-secret',
28+
birthdate=date(1987, 2, 15),
29+
registered_at=datetime(2022, 10, 31, 10, 13, 1),
30+
).save()
31+
32+
self.assertEqual(
33+
dumps([{'id': 1, 'name': 'Brandon', 'birthdate': '1987-02-15', 'registered_at': '2022-10-31T10:13:01'}]),
34+
self.encode(User.objects.all())
35+
)

inertia/tests/testapp/models.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from django.db import models
2+
3+
class User(models.Model):
4+
name = models.CharField(max_length=255)
5+
password = models.CharField(max_length=255)
6+
birthdate = models.DateField()
7+
registered_at = models.DateTimeField()

inertia/tests/testapp/views.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
from django.http.response import HttpResponse
22
from django.shortcuts import redirect
33
from django.utils.decorators import decorator_from_middleware
4-
from inertia.http import inertia, render
5-
from inertia.utils import lazy
6-
from inertia.share import share
4+
from inertia import inertia, render, lazy, share
75

86
class ShareMiddleware:
97
def __init__(self, get_response):

0 commit comments

Comments
 (0)