On Wed, Oct 25, 2017 at 6:14 PM, sqlalchemy_mysql wrote:
> I understand default value is used to emit Insert statement. Is there any
> way to have default value is accessible via python object. See below example
>
class User(Base):
> ... __tablename__ = 'users'
> ... id = Column(Integer, primary_key=True)
> ... name = Column(String)
> ... password = Column(String, default='welcome')
> ...
ed_user = User(name='ed')
print(ed_user.name)
> ed
print(ed_user.password)
> None
>
> Expect `print(ed_user.password)` to return 'welcome' in this case.
>
> Adding it to a session, flush and query the object will return default value
> but is there a way to get default value without re-querying the object?
there is no magic solution to this, you'd either alter your User
object to set it within your constructor (e.g. __init__), or
alternatively you can use the event handler init() to set it:
http://docs.sqlalchemy.org/en/latest/orm/events.html?highlight=instanceevents%20init#sqlalchemy.orm.events.InstanceEvents.init
@event.listens_for(User, "init")
def _set_password(target, args, kwargs):
kwargs.setdefault("password", "welcome")
>
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full
> description.
> ---
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
http://www.sqlalchemy.org/
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable
Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.