[英]MongoAlchemy: One of two different DocumentField's
Using MongoAlchemy , is it possible to have a DocumentField that can be one of two types? 使用MongoAlchemy ,是否可以使DocumentField成为两种类型之一? Eg: 例如:
class A(Document):
foo = StringField()
class B(Document):
bar = StringField()
class C(Document):
child = DocumentField(A or B)
I thought of a few options that might work: 我想到了一些可行的选择:
A
and B
a common parent and then do child = DocumentField(CommonParent)
. 给A
和B
一个共同的父母,然后让child = DocumentField(CommonParent)
。 Field
that overrides DocumentField
, but changes the validator to search through a list of types, instead of one. 编写一个重写DocumentField
的自定义Field
,但更改验证器以搜索类型列表,而不是一个。 AnythingField
. 只需使用AnythingField
。 Kinda defeats the point. 金达(Kinda)败露了这一点。 But wondered if it was already done? 但想知道它是否已经完成?
There was a branch that I never merged (although I did use it for a while) where I implemented polymorphic types: 有一个我从未合并过的分支(尽管我确实使用了一段时间)在其中实现了多态类型:
https://github.com/jeffjenkins/MongoAlchemy/tree/poly-queries https://github.com/jeffjenkins/MongoAlchemy/tree/poly-queries
If you're willing to do a bit of bug fixing that's a pretty good option. 如果您愿意做一些错误修复,那是一个不错的选择。
Otherwise the easiest thing to do is implement a custom field. 否则,最简单的方法是实现自定义字段。 I suspect it might be easier to create a regular field that took a list of DocumentField
s as inputs and which could distinguish than to mess with DocumentField (which is sort of what the polymorphism branch does, but more complexly). 我怀疑创建一个以DocumentField
的列表作为输入并且可以区分的常规字段可能比与DocumentField搞乱(这是多态分支的功能,但更复杂)更容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.