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