|
2 | 2 |
|
3 | 3 | Check out the [IDOM Core docs](https://idom-docs.herokuapp.com/docs/reference/hooks-api.html?highlight=hooks) on hooks!
|
4 | 4 |
|
| 5 | +## Use Query |
| 6 | + |
| 7 | +The `use_query` hook is used fetch Django ORM queries. |
| 8 | + |
| 9 | +=== "components.py" |
| 10 | + |
| 11 | + ```python |
| 12 | + from example_project.my_app.models import TodoItem |
| 13 | + from idom import component, html |
| 14 | + from django_idom.hooks import use_query |
| 15 | + |
| 16 | + def get_items(): |
| 17 | + return TodoItem.objects.all() |
| 18 | + |
| 19 | + @component |
| 20 | + def todo_list(): |
| 21 | + item_query = use_query(get_items) |
| 22 | + |
| 23 | + if item_query.loading: |
| 24 | + rendered_items = html.h2("Loading...") |
| 25 | + elif item_query.error: |
| 26 | + rendered_items = html.h2("Error when loading!") |
| 27 | + else: |
| 28 | + rendered_items = html.ul(html.li(item, key=item) for item in item_query.data) |
| 29 | + |
| 30 | + return rendered_items |
| 31 | + ``` |
| 32 | + |
| 33 | +=== "models.py" |
| 34 | + |
| 35 | + ```python |
| 36 | + from django.db import models |
| 37 | + |
| 38 | + class TodoItem(models.Model): |
| 39 | + text = models.CharField(max_length=255) |
| 40 | + ``` |
| 41 | + |
| 42 | +??? question "Can 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. |
| 53 | + |
| 54 | +## Use Mutation |
| 55 | + |
| 56 | +The `use_mutation` hook is used to modify Django ORM objects. |
| 57 | + |
| 58 | +=== "components.py" |
| 59 | + |
| 60 | + ```python |
| 61 | + from example_project.my_app.models import TodoItem |
| 62 | + from idom import component, html |
| 63 | + from django_idom.hooks import use_mutation |
| 64 | + |
| 65 | + def add_item(text: str): |
| 66 | + TodoItem(text=text).save() |
| 67 | + |
| 68 | + @component |
| 69 | + def todo_list(): |
| 70 | + item_mutation = use_mutation(add_item) |
| 71 | + |
| 72 | + if item_mutation.loading: |
| 73 | + mutation_status = html.h2("Adding...") |
| 74 | + elif item_mutation.error: |
| 75 | + mutation_status = html.h2("Error when adding!") |
| 76 | + else: |
| 77 | + mutation_status = "" |
| 78 | + |
| 79 | + def submit_event(event): |
| 80 | + if event["key"] == "Enter": |
| 81 | + item_mutation.execute(text=event["target"]["value"]) |
| 82 | + |
| 83 | + return html.div( |
| 84 | + html.label("Add an item:"), |
| 85 | + html.input({"type": "text", "onKeyDown": submit_event}), |
| 86 | + mutation_status, |
| 87 | + ) |
| 88 | + ``` |
| 89 | + |
| 90 | +=== "models.py" |
| 91 | + |
| 92 | + ```python |
| 93 | + from django.db import models |
| 94 | + |
| 95 | + class TodoItem(models.Model): |
| 96 | + text = models.CharField(max_length=255) |
| 97 | + ``` |
| 98 | + |
| 99 | +??? question "Can `use_mutation` trigger a refetch of `use_query`?" |
| 100 | + |
| 101 | + Yes, `use_mutation` can queue a refetch of a `use_query` via the `refetch=...` argument. |
| 102 | + |
| 103 | + The example below is a merge of the `use_query` and `use_mutation` examples above with the addition of a `refetch` argument on `use_mutation`. |
| 104 | + |
| 105 | + Please note that any `use_query` hooks that use `get_items` will be refetched upon a successful mutation. |
| 106 | + |
| 107 | + ```python title="components.py" |
| 108 | + from example_project.my_app.models import TodoItem |
| 109 | + from idom import component, html |
| 110 | + from django_idom.hooks import use_mutation |
| 111 | + |
| 112 | + def get_items(): |
| 113 | + return TodoItem.objects.all() |
| 114 | + |
| 115 | + def add_item(text: str): |
| 116 | + TodoItem(text=text).save() |
| 117 | + |
| 118 | + @component |
| 119 | + def todo_list(): |
| 120 | + item_query = use_query(get_items) |
| 121 | + if item_query.loading: |
| 122 | + rendered_items = html.h2("Loading...") |
| 123 | + elif item_query.error: |
| 124 | + rendered_items = html.h2("Error when loading!") |
| 125 | + else: |
| 126 | + rendered_items = html.ul(html.li(item, key=item) for item in item_query.data) |
| 127 | + |
| 128 | + item_mutation = use_mutation(add_item, refetch=get_items) |
| 129 | + if item_mutation.loading: |
| 130 | + mutation_status = html.h2("Adding...") |
| 131 | + elif item_mutation.error: |
| 132 | + mutation_status = html.h2("Error when adding!") |
| 133 | + else: |
| 134 | + mutation_status = "" |
| 135 | + |
| 136 | + def submit_event(event): |
| 137 | + if event["key"] == "Enter": |
| 138 | + item_mutation.execute(text=event["target"]["value"]) |
| 139 | + |
| 140 | + return html.div( |
| 141 | + html.label("Add an item:"), |
| 142 | + html.input({"type": "text", "onKeyDown": submit_event}), |
| 143 | + mutation_status, |
| 144 | + rendered_items, |
| 145 | + ) |
| 146 | + ``` |
| 147 | + |
| 148 | +??? question "Can I make ORM calls without hooks?" |
| 149 | + |
| 150 | + Due to Django's ORM design, database queries must be deferred using hooks. Otherwise, you will see a `SynchronousOnlyOperation` exception. |
| 151 | + |
| 152 | + This may be resolved in a future version of Django with a natively asynchronous ORM. |
| 153 | + |
| 154 | + However, even when resolved it is best practice to perform ORM queries within the `use_query` in order to handle `loading` and `error` states. |
| 155 | + |
| 156 | +??? question "What is an "ORM"?" |
| 157 | + |
| 158 | + A Python **Object Relational Mapper** is an API for your code to access a database. |
| 159 | + |
| 160 | + See the [Django ORM documentation](https://docs.djangoproject.com/en/dev/topics/db/queries/) for more information. |
| 161 | + |
5 | 162 | ## Use Websocket
|
6 | 163 |
|
7 | 164 | You can fetch the Django Channels websocket at any time by using `use_websocket`.
|
|
0 commit comments