Passing a large query through a Django template #118
-
This may be Django-specific. Let's say I have a view that summons a Django template to a display a long table of widgets, where each widget is an idom component (correponding to a record from a DB table). As far as I understand, I can pass at most JSONs from a template to a component via Thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 15 replies
-
If you need "shared data" between several components, you need to create one parent component to contain all your sub-components. You should use |
Beta Was this translation helpful? Give feedback.
-
Instead of defining the outer table with a template in Django, it would probably be best to do so using IDOM. This way, you can define an outer component that performs one large query and returns a table of widgets where the data from that query is disseminated to those widgets. As mentioned in this answer, you can pass data from that query in two ways - either passing the data down to the components with arguments, or if the components which actually consume the data are sufficiently deep such that passing arguments would be tedious, you can With all that said, here's what that outer table component could look like with and without using a context: Without context from idom import component, html
from django_idom import use_query
@component
def Table():
data_query = use_query(get_the_data)
if data_query.loading:
return html.h2("Loading...")
elif data_query.error:
return html.h2("Error!")
return html.table(
Row(row, key=row.some_unique_id_from_your_data)
for row in data_query.data
)
@component
def Row(row_data):
return html.tr(...) # render a table row somehow
def get_the_data():
... Using context from idom import component, html, create_context, use_context
from django_idom import use_query
TableDataContext = create_context(None)
@component
def Table():
data_query = use_query(get_the_data)
if data_query.loading:
return html.h2("Loading...")
elif data_query.error:
return html.h2("Error!")
return TableDataContext(
html.table(
RowContainerOuter(key=row.some_unique_id_from_your_data)
for row in data_query.data
),
value=data_query.data
)
@component
def RowContainerOuter():
return html.tr(RowContainerInner())
@component
def RowContainerInner():
return html.tr(Row())
@component
def Row():
row_data = use_context(TableDataContext)
return ... # render a table row somehow
def get_the_data():
... |
Beta Was this translation helpful? Give feedback.
-
Thank you both for the answers. Yes, I considered doing it this way, @rmorshea, but I think this is leaving too much Django functionality on the table. Anyway, I looked 'under the hood', and my issue seems more related to django-idom==2.1.0. Let me know if I should move it there somehow. So, I saw that the template tag component_instance = component_constructor(**component_kwargs) I'd like to have an option to pass the Django _register_component(dotted_path) would be _register_component(context, dotted_path) and therein, IDOM_REGISTERED_COMPONENTS[dotted_path] = partial(_import_dotted_path(dotted_path), context) What do you think? Since I can pass anything to an idom component, I don't see why it shouldn't be possible for a template tag. |
Beta Was this translation helpful? Give feedback.
-
This limitation will be resolved in v3.0.0 Template tag parameters will now have unrestricted See the following PR for more details: |
Beta Was this translation helpful? Give feedback.
This limitation will be resolved in v3.0.0
Template tag parameters will now have unrestricted
arg
/kwarg
values. Thus, they will perform identically to a typical Django template tag.See the following PR for more details: