Comments (8)
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
from reactpy-django.
With that kind of interface, how would someone edit an entry and then save changes?
There needs to be some way of performing .save()
operations via database_sync_to_async
from reactpy-django.
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?
from reactpy-django.
You're right though, that does feel a bit disjointed. Maybe the interface ought to be more like use_state
for single record access.
model, set_model = use_one_model(
Model.objects.get(...) # assuming query exec is delayed
)
async def handle_event():
await set_model(attr=...)
from reactpy-django.
I'm not really sure how this should work for bulk access though...
from reactpy-django.
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
in a use_memo
hook:
model, set_model = use_one_model(...)
something_expensive = use_memo(lambda: compute_something_expensive(model))
A naive implementation of use_one_model
would mutate model
, call model.save()
and, on the next render, return the same model
instance. But in order to know whether something_expensive
needs to be re-computed, IDOM looks at the identity of model
. If model
is the same object, then something_expensive
won't be re-computed. Thus you'd encounter a bug where, despite calling set_model
, something_expensive
would not receive a corresponding update.
It almost feels like there needs to be a thin declarative wrapper around Django's models...
from reactpy-django.
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 Model.objects
needs to occur within a sync context.
I honestly think the best solution to this might end up being a monkey patch to the Django Model
object...
from reactpy-django.
Closing this.
I think it no longer makes sense given the existence of use_query
, and may not be technologically feasible.
from reactpy-django.
Related Issues (20)
- Improving use_query HOT 24
- `use_query` ManyToMany fetching sometimes fails
- Databased-backed component `args`/`kwargs` HOT 8
- `use_query` doesn't work with fields using modified `related_name`
- Remove Accidentally Committed File HOT 1
- `use_mutation` and `use_query` are not async HOT 1
- `use_query` has no `refetch` option. HOT 1
- Add rename warning to legacy `django-idom` HOT 4
- `django_router` component HOT 2
- Priority of applications in Django
- Add "HTTP Mode" to ReactPy-Django HOT 1
- Add a cache decorator HOT 1
- Add encryption support to `use_user_data` hook
- Allow `django_css` and `django_js` to load `only_once` HOT 2
- Add offline fallback to component template tags.
- `use_session` hook HOT 1
- Support Jinja2 templates
- Error while using django-tenants package
- Components don't load with `django-tenants`
- `use_root_id` hook
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from reactpy-django.