Hi Mike,
Instead of this:
class Entity:
def set_timestamp(self, timestamp, timezone):
# ...
which you have to manually call it to set, I'd like to have an automated
setting directly on initiation, so that I can create new records in a more
uniformed ways:
Entity(**params_from_web_form)
Now I just tried using @hybrid_property.setter:
class Tutorsession(Base):
...
@start_time_local.setter
def start_time_local(self, value):
timezone_stamp =
json.loads(cherrypy.request.body.read())['timezone_stamp']
self.start_time = create_timestamp(value, timezone_stamp)
unfortunately it gives me an error:
TypeError: 'start_time_local' is an invalid keyword argument for
Turtorsession
Is hybrid_property.setter allowed to create new records like this way at
all?
On Saturday, September 4, 2021 at 7:43:24 AM UTC-7 Mike Bayer wrote:
>
>
> On Fri, Sep 3, 2021, at 4:07 PM, [email protected] wrote:
>
> In the official documentation it says:
>
>
> *validates*
> <https://docs.sqlalchemy.org/en/13/orm/mapped_attributes.html#sqlalchemy.orm.validates>(*names,
>
> **kw)
>
> Decorate a method as a ‘validator’ for one or more named properties.
>
>
>
> I need to validate two incoming *-**attributes at the same time, for
> example:
>
>
> class Entity(Base):
>
> ....
>
> attr_timezone = Column(String)
>
> attr_timestamp = Column(MyTimeStamp)
>
>
> When taking incoming arguments, `attr_timezone` as a string need to be
> first validated and converted to timezone instance before being attached to
> my custom class `MyTimeStamp`. Can `validates` do this?
>
>
> its not a simple yes or no because the use case does not totally make
> sense.
>
> what happens if someone does this:
>
> some_entity().attr_timestamp = <some timestmap>
>
> and then nothing else? what do you want to happen when that attribute set
> occurs?
>
>
> what you probably want to do is require that one or the other attribute is
> set first. you can certainly use @validates to set this up, it's just an
> event hook.
>
> more realistically though in the most practical sense, you'd want to have
> a method that clearly documents what needs to be done:
>
> class Entity:
> def set_timestamp(self, timestamp, timezone):
> # ...
>
>
> then make "attr_timestamp" and "attr_timezone" private. This is boring
> and not magical but if I were in a large organization where we just need it
> to be clear and unambigious, this is how I'd do it.
>
>
>
>
>
>
> --
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sqlalchemy/ee2abfce-6a5a-4d43-b508-f86be3ea8cd5n%40googlegroups.com
>
> <https://groups.google.com/d/msgid/sqlalchemy/ee2abfce-6a5a-4d43-b508-f86be3ea8cd5n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
>
>
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/sqlalchemy/65671e2f-094a-427a-89a4-2678c9e6125en%40googlegroups.com.