Code Monkey home page Code Monkey logo

Comments (8)

rmorshea avatar rmorshea commented on May 28, 2024

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.

Archmonger avatar Archmonger commented on May 28, 2024

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.

rmorshea avatar rmorshea commented on May 28, 2024

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.

rmorshea avatar rmorshea commented on May 28, 2024

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.

rmorshea avatar rmorshea commented on May 28, 2024

I'm not really sure how this should work for bulk access though...

from reactpy-django.

rmorshea avatar rmorshea commented on May 28, 2024

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.

Archmonger avatar Archmonger commented on May 28, 2024

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.

Archmonger avatar Archmonger commented on May 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.