0
でのモデルとは異なり、レコードの挿入を防止する方法:いくつかのハック来る私は、以下のようにいくつかのプリミティブと配列型のプロパティを持つデータモデル持っているのpython
class WordCollection:
"""First model"""
def __init__(self, **properties):
self.name = ""
self.status = CommonStatus.active
self.expire_time = time.time() + 1000 * 60 * 24 # after 1 day
self.created_date = time.time()
self.words = []
self.__dict__.update(properties)
を。例えば、クラスの一部ではないプロパティでクラスを構築する場合、簡単にハックすることができます。
collection = WordCollection(**{..., "hack_property":"large text or irrelative data"})
ので、私はクラスにプレイしたメソッドを初期化します。
class WordCollection:
"""Second model"""
def __init__(self, **properties):
self.name = properties["name"] if "name" in properties else ""
self.status = properties["active"] if "active" in properties else CommonStatus.active
self.expire_time = properties["expire_time"] if "expire_time" in properties else time.time() + 1000 * 60 * 24 # after 1 day
self.created_date = properties["created_date"] if "created_date" in properties else time.time()
self.words = properties["words"] if "words" in properties else []
しかし、上記のコードは、完全な形で問題を解決しない:
:collection = WordCollection(**{..., "name":{"hack_property":"large text or irrelative data"}})
これが最後の再構築コードです
class WordCollection:
"""Third Model"""
def __init__(self, **properties):
self.name = properties["name"] if "name" in properties and isinstance(properties["name"], str) else ""
self.status = properties["active"] if "active" in properties \
and isinstance(properties["status"], int) else CommonStatus.active
....
上記の改訂履歴それは条件付きの複雑さをもたらし、私はそれがより良い解決策であると信じています。
オプス申し訳ありませんpymongoは間違ったタグです。私はそれを削除してしまった。 – RockOnGom