Coder Social home page Coder Social logo

Comments (10)

baverman avatar baverman commented on July 21, 2024

Closed by 95ee8eb for #64

from flask-admin.

smlynch avatar smlynch commented on July 21, 2024

I ran into an issue trying to set a required property on my model using the new on_model_change callback because self.session.flush() is called before it. What is the reason an explicit flush is called before the callback, and can it either be removed or moved to after the callback?

from flask-admin.

mrjoes avatar mrjoes commented on July 21, 2024

You're right - you should have chance to modify something in the callback.

from flask-admin.

techniq avatar techniq commented on July 21, 2024

Great, thanks (I would have submitted a pull request, but wasn't sure the reasoning behind the flush). Is the explicit flush even needed here?

from flask-admin.

baverman avatar baverman commented on July 21, 2024

Is the explicit flush even needed here?

Yeah. That's the main point for the whole callback. After flush model has an "id" and one can handle update/create cases in generic way. For example you can fill depended models with created foreign key.

from flask-admin.

baverman avatar baverman commented on July 21, 2024

I ran into an issue trying to set a required property on my model using the new on_model_change callback because self.session.flush() is called before it.

Use create/update_model.

from flask-admin.

techniq avatar techniq commented on July 21, 2024

Hmm, I guess this conflicts with my intended use of on_model_change for setting other required fields on the model not by the form before committing

If I override create_model/update_model, I would have to completely override it's implementation (copy/paste).

Maybe instead of 1 callback/event (on_model_change), there is before_model_change and after_model_change. Since this hasn't been part of a release yet, it wouldn't break compatibility (except for us running off git).

from flask-admin.

mrjoes avatar mrjoes commented on July 21, 2024

I think we should omit flushing for these callbacks: there are cases when you need to modify model before it will be saved. And for some other cases it will work without explicit flushing.

SQLAlchemy is smart enough to flush dependencies in most of the cases: https://gist.github.com/3941123

However, if you will try to access user.id without flushing, it will be set to None.

I think it is OK, as new version of flask-admin is not yet released and it is possible to mention in documentation that callback happens before model is comitted, so if you need id in on_model_change, flush session.

Adding another on_before_model_change does not look pretty to me.

Thoughts?

from flask-admin.

baverman avatar baverman commented on July 21, 2024

Sorry, I forgot that overriding of create_model have no any sense.

Maybe instead of 1 callback/event (on_model_change), there is before_model_change and after_model_change

Boo. Very bad idea.

I agree. Removing flush is the best solution. Clients still can call it by themselves.

from flask-admin.

techniq avatar techniq commented on July 21, 2024

I agree as well.

from flask-admin.

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.