Hi,

I have a pattern in some models where there is a private (as in prefixed 
with __) relationship and then a property to handle some things that need 
to happen when the relationship is fetched or written to.

Currently it's implemented like this:
class ModelClass(Base):
  __field_for_relationship = relationship('OtherModel')
  @property
  def field_for_relationship(self):
    # Some custom logic
    return self.__field_for_relationship
  
  @field_for_relationship.setter
  def field_for_relationship(self, value: OtherModel):
    # Some custom logic.
    self.__field_for_relationship = value

This works, but I'd like to DRY this up into some kind of nice one-liner if 
possible because I'm using this pattern on a handful of relationships. 
However, it seems like the relationship field itself needs to be on the 
model because of some magic that happens under the hood.

Any ideas on if it's possible to combine this into some kind of reusable 
utility to DRY it up?

I'm not sure how important it is, but I'm still on 1.4.x, haven't made the 
jump to 2.x yet.

Thanks!
Tony

-- 
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/b285bdb1-c7b9-4734-9a5b-fe937241b44an%40googlegroups.com.

Reply via email to