Skip to content

Commit 96c451f

Browse files
committed
set fetch attributes to false by default
1 parent d1bd3f8 commit 96c451f

File tree

4 files changed

+9
-41
lines changed

4 files changed

+9
-41
lines changed

src/django_idom/hooks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
from django_idom.types import (
1818
IdomWebsocket,
1919
Mutation,
20-
QueryOptions,
2120
Query,
21+
QueryOptions,
2222
_Params,
2323
_Result,
2424
)
@@ -203,7 +203,7 @@ def reset() -> None:
203203
def _postprocess_django_query(fetch_cls: QueryOptions) -> None:
204204
"""Recursively fetch all fields within a `Model` or `QuerySet` to ensure they are not performed lazily.
205205
206-
Some behaviors can be modified through `OrmFetch` attributes."""
206+
Some behaviors can be modified through `query_options` attributes."""
207207
data = fetch_cls._data
208208

209209
# `QuerySet`, which is effectively a list of `Model` instances

src/django_idom/types.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ class ViewComponentIframe:
5757
class QueryOptions:
5858
"""A Django ORM query function, alongside some configuration values."""
5959

60+
def __call__(self, *args, **kwargs):
61+
return self.func(*args, **kwargs)
62+
6063
func: Callable
6164
"""Callable that fetches ORM object(s)."""
6265

@@ -65,10 +68,10 @@ class QueryOptions:
6568
This is only intended to be set automatically by the `use_query` hook."""
6669

6770
postprocessor_options: dict[str, Any] = field(
68-
default_factory=lambda: {"many_to_many": True, "many_to_one": True}
71+
default_factory=lambda: {"many_to_many": False, "many_to_one": False}
6972
)
7073
"""Configuration values usable by the `postprocessor`."""
7174

7275
postprocessor: Callable[[QueryOptions], None] | None = None
73-
"""A post processing callable that can read/modify the `OrmFetch` object. If unset, the default fetch
76+
"""A post processing callable that can read/modify the `QueryOptions` object. If unset, the default fetch
7477
handler is used to prevent lazy loading of Django fields."""

tests/test_app/components.py

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,12 @@ def create_relational_parent() -> RelationalParent:
166166
return parent
167167

168168

169+
@query_options(many_to_many=True, many_to_one=True)
169170
def get_relational_parent_query():
170171
return RelationalParent.objects.first() or create_relational_parent()
171172

172173

174+
@query_options(many_to_many=True, many_to_one=True)
173175
def get_foriegn_child_query():
174176
child = ForiegnChild.objects.first()
175177
if not child:
@@ -207,41 +209,6 @@ def relational_query():
207209
)
208210

209211

210-
@query_options(many_to_many=False, many_to_one=False)
211-
def get_relational_parent_query_fail():
212-
return RelationalParent.objects.first() or create_relational_parent()
213-
214-
215-
@component
216-
def relational_query_fail_mtm():
217-
relational_parent = use_query(get_relational_parent_query_fail)
218-
219-
if not relational_parent.data:
220-
return
221-
222-
return html.div(
223-
{"id": "relational-query-fail-mtm"},
224-
html.div(
225-
f"Relational Parent Many To Many Fail: {relational_parent.data.many_to_many.all()}"
226-
),
227-
)
228-
229-
230-
@component
231-
def relational_query_fail_mto():
232-
relational_parent = use_query(get_relational_parent_query_fail)
233-
234-
if not relational_parent.data:
235-
return
236-
237-
return html.div(
238-
{"id": "relational-query-fail-mto"},
239-
html.div(
240-
f"Relational Parent Many to One: {relational_parent.data.foriegnchild_set.all()}"
241-
),
242-
)
243-
244-
245212
def get_todo_query():
246213
return TodoItem.objects.all()
247214

tests/test_app/templates/base.html

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ <h1>IDOM Test Page</h1>
3232
<div>{% component "test_app.components.django_js" %}</div>
3333
<div>{% component "test_app.components.unauthorized_user" %}</div>
3434
<div>{% component "test_app.components.authorized_user" %}</div>
35-
<div>{% component "test_app.components.relational_query_fail_mtm" %}</div>
36-
<div>{% component "test_app.components.relational_query_fail_mto" %}</div>
3735
<div>{% component "test_app.components.relational_query" %}</div>
3836
<div>{% component "test_app.components.todo_list" %}</div>
3937
<div>{% component "test_app.components.view_to_component_sync_func" %}</div>

0 commit comments

Comments
 (0)