-
-
Notifications
You must be signed in to change notification settings - Fork 22
use_database_state
hook
#39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I imagine the usage should look like either: results = use_database(lambda: Model.objects.all())
@use_database
def results():
return Model.objects.all() The implementation might look like: def use_database(function, initial_value=None):
result, set_result = use_state(initial_value)
query_function = use_callback(query_function)
async_function = use_memo(lambda: database_sync_to_async(function))
@use_effect
async def exec_function():
output = await async_function()
if isinstance(output, QuerySet):
bool(output)
set_result(output)
return result |
With that kind of interface, how would someone edit an entry and then save changes? There needs to be some way of performing |
Wouldn't one typically do that in response to some sort of event, in which case you'd be able to define an async handler that does this? |
You're right though, that does feel a bit disjointed. Maybe the interface ought to be more like model, set_model = use_one_model(
Model.objects.get(...) # assuming query exec is delayed
)
async def handle_event():
await set_model(attr=...) |
I'm not really sure how this should work for bulk access though... |
Actually, there's a deeper issue here around object identity where the declarative nature of IDOM and the imperative nature of Django clash - Django expects you to modify an object to update it in the database, but IDOM uses object identity to infer whether something has changed. For example, imagine using a model, set_model = use_one_model(...)
something_expensive = use_memo(lambda: compute_something_expensive(model)) A naive implementation of It almost feels like there needs to be a thin declarative wrapper around Django's models... |
While Django models might be mutable (haven't confirmed), I know that Django querysets are immutable. The lazy nature of things is the biggest issue though. I haven't been able to find a good solution to that yet. Also, I believe accessing I honestly think the best solution to this might end up being a monkey patch to the Django |
Closing this. I think it no longer makes sense given the existence of |
Old Behavior
State is generated on a per-render basis and has no method of being permanently stored.
Additionally, there is no convenient way to perform DB queries within components (as of Django 4.1)
New Behavior
Allow for persistent state that is databased backed.
Implementation Details
Persistently store/restore context based on a database model.
The interface should take in a single database object...
In the background, the set state is doing the following...
Code of Conduct
The text was updated successfully, but these errors were encountered: