I have the following two classes:
class user(Base):
....
id=Column(Integer, primary_key=True)
address=relationship('Address',backref='user')
class Address(Base):
...
IsItAssgined=False
user_id=Column(Integer, ForeignKey('user.id'))
Lets create two instances of these classes:
Addr=Address()
Tom=User()
I would like the following behaviour: upon appending the Addr to the User, The field IsItAssigned in the Address gets updated to True, i.e.
Tom.address.append(Addr)
print Addr.IsItAssigned
-> to be True
I don’t know how to use descriptors here if possible.
I’d use a property for that, one that tests if the
user_idcolumn is not NULL. Acolumn_property()seems appropriate here:Now
IsItAssignedwill be eitherTrueorFalse. Alternatively, use a hybrid property:A hybrid property has the advantage of being usable in a query as well. Last but not least, for this case, perhaps an ordinary property would do too, it’ll only work on already loaded instances though:
I’d use a different name, though.
assignedmight be better; the Python style guide (PEP 8) advocates thelower_case_with_underscoresfor methods and instance attributes.