Skip to content

Commit afad233

Browse files
authored
ORM Usage Docs (#87)
1 parent 9f0fa65 commit afad233

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

.github/pull_request_template.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# Description
1+
## Description
22

33
A summary of the changes.
44

5-
# Checklist:
5+
## Checklist:
66

77
Please update this checklist as you complete each item:
88

docs/features/hooks.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ def my_component():
3434

3535
??? info "This hook's behavior will be changed in a future update"
3636

37-
This hook will be updated to return the browser's current URL. This change will come in alongside [IDOM URL routing support](https://github.com/idom-team/idom/issues/569).
37+
This hook will be updated to return the browser's current URL. This change will come in alongside IDOM URL routing support.
38+
39+
Check out [idom-team/idom#569](https://github.com/idom-team/idom/issues/569) for more information.
3840

3941
This is a shortcut that returns the Websocket's `path`.
4042

docs/features/orm.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
??? info "Our suggested ORM access method will be changed in a future update"
2+
3+
The Django IDOM team is currently assessing the optimal way to integrate the [Django ORM](https://docs.djangoproject.com/en/dev/topics/db/queries/) with our React-style framework.
4+
5+
This docs page exists to demonstrate how the ORM should be used with the current version of Django IDOM.
6+
7+
Check out [idom-team/django-idom#79](https://github.com/idom-team/django-idom/issues/79) for more information.
8+
9+
This is the suggested method of using the Django ORM with your components.
10+
11+
```python title="components.py"
12+
from channels.db import database_sync_to_async
13+
from example_project.my_app.models import Category
14+
from idom import component, hooks, html
15+
16+
17+
@component
18+
def simple_list():
19+
categories, set_categories = hooks.use_state(None)
20+
21+
@hooks.use_effect
22+
@database_sync_to_async
23+
def get_categories():
24+
if categories:
25+
return
26+
set_categories(list(Category.objects.all()))
27+
28+
if not categories:
29+
return html.h2("Loading...")
30+
31+
return html.ul(
32+
[html.li(category.name, key=category.name) for category in categories]
33+
)
34+
```
35+
36+
??? question "Why does this example use `list()` within `set_categories`?"
37+
38+
[Django's ORM is lazy](https://docs.djangoproject.com/en/dev/topics/db/queries/#querysets-are-lazy). Thus, `list()` is used to ensure that the database query is executed while within the hook.
39+
40+
Failure to do this will result in `SynchronousOnlyOperation` when attempting to access your data.
41+
42+
??? question "Why can't I make ORM calls without hooks?"
43+
44+
Due to Django's ORM design, database queries must be deferred using hooks. Otherwise, you will see a `SynchronousOnlyOperation` exception.
45+
46+
This may be resolved in a future version of Django with a natively asynchronous ORM.
47+
48+
??? question "What is an ORM?"
49+
50+
A Python **Object Relational Mapper** is an API for your code to access a database.
51+
52+
See the [Django ORM documentation](https://docs.djangoproject.com/en/dev/topics/db/queries/) for more information.

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ nav:
1111
- Exclusive Features:
1212
- Components: features/components.md
1313
- Hooks: features/hooks.md
14+
- ORM: features/orm.md
1415
- Template Tag: features/templatetag.md
1516
- Settings: features/settings.md
1617
- Contribute:

0 commit comments

Comments
 (0)