I have the following relationship set in the model:
role_profiles = Table('roleprofile', Base.metadata, Column('role_id', Integer, ForeignKey('role.id')), Column('profile_id', Integer, ForeignKey('profile.id')) ) class profile(Base): __tablename__ = 'profile' # Columns... roles = relationship('role', secondary=role_profiles, backref='profiles') class role(Base): __tablename__ = 'role' # Columns...
So, now I understand that it works, since the role property in the profile object will contain a list of role classes (what it does).
I want to serialize for each property of the model class as a whole. It works fine for a top class profile, and I determine that there is a roles list that I have to register with:
The problem is that the backref relationship adds a pointer back to the profile. Then the same profile is serialized and repeats the roles over and over until stack overflow .
Is there a way to determine that the property is backref added by relationship ?
Update
Maybe I should add that it works fine in this case if I remove the backref since I don't need it, but I would like to keep it.
Update
As a temporary fix, I added a class property to the base class:
class BaseModelMixin(object): """Base mixin for models using stamped data""" __backref__ = None
and add it like this:
class role(Base): __tablename__ = 'role' __backref__ = ('profiles', )
and use it in my recursion:
if self.__backref__ and property_name in self.__backref__: continue
If there is a better way, let me know because it does not look optimal.