Determine if a property is a backref
I have the following relationship set up in a 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 as I now understand that it works is that the roles property on the
profile object will contain a list of role classes (which it does).
What I want to do is to serialize for each property of the model class
generically. It works fine for the top class profile and I determine that
there is a list of roles that I should recurse into:
# I need a statement here to check if the field.value is a backref
#if field.value is backref:
# continue
if isinstance(field.value, list):
# Get the json for the list
value = serialize.serialize_to_json(field.value)
else:
# Get the json for the value
value = cls._serialize(field.value)
The problem is that the backref of the relationship adds a pointer back to
the profile. The same profile is then serialized and it recurse the roles
over and over again until stack overflow.
Is there a way to determine that the property is a backref added by the
relationship?
No comments:
Post a Comment