I need create relationship with custom getter and setter. It would be nice to define setter/getter parameters on relationship declaration Also I need alter relation getter even if no data are joined (i.e. change None to something more complex)
Here is roughly what I need:
class Address(Base):
#...
#hybrid attributes? custom class instrumentation? I need call this function even if no rows are joined to user or company
def getter(self):
#for each joined row
return {'address': "%s %s" % (self.cilty, self.country), 'maximum' : some_data['maximum']}
#if no row joined
return {'address': None, 'maximum' : some_data['maximum']}
def setter(self, user):
if some_data['only_country'] != user.country:
raise Exception('You can`t live in %s. sorry', (user.country,))
self.country = user.country
class User(Base):
#...
addresses = relationship(Address, some_data = {'maximum': 1, only_country: 'UA'})
class Company(Base):
#...
addresses = relationship(Address, some_data = {'maximum': 7, only_country: 'US'})
Now i want to use this relationship as follows
u = db.query(User).filter_by(id=1).one()
# I need data from getter here
print(u.address)
u = User()
# I need setter called here with references to user and some_data defined in relationship definition
u.address = Address()
Aucun commentaire:
Enregistrer un commentaire