On Fri, Sep 3, 2021, at 4:07 PM, niuji...@gmail.com 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 sqlalchemy+unsubscr...@googlegroups.com.
> 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 sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/71db7894-13f6-4855-94d9-ec1107ba69a2%40www.fastmail.com.

Reply via email to