モデルに次の関係を設定しています。
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...
そのため、プロファイル オブジェクトのロール プロパティにはロール クラスのリストが含まれていることがわかりました (実際に含まれています)。
私がやりたいことは、モデル クラスの各プロパティを一般的にシリアル化することです。roles
トップクラスのプロファイルでは問題なく動作し、再帰する必要があるリストがあると判断しました。
# 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)
問題はbackref
、関係の がポインタをプロファイルに追加することです。次に、同じプロファイルがシリアル化され、 までロールを何度も再帰しますstack overflow
。
プロパティがbackref
によって追加されたことを確認する方法はありrelationship
ますか?
アップデート
この場合、backref
不要なので削除しても問題なく動作することを追加する必要があるかもしれませんが、そのままにしておきます。
アップデート
一時的な修正として、基本クラスにクラス プロパティを追加しました。
class BaseModelMixin(object):
"""Base mixin for models using stamped data"""
__backref__ = None
次のように追加します。
class role(Base):
__tablename__ = 'role'
__backref__ = ('profiles', )
# Columns...
私の再帰では次のように使用します。
if self.__backref__ and property_name in self.__backref__:
continue
これは最適に見えないため、より良い方法があれば教えてください。