简体   繁体   English

MongoAlchemy:两个不同DocumentField之一

[英]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: 我想到了一些可行的选择:

  • Give A and B a common parent and then do child = DocumentField(CommonParent) . AB一个共同的父母,然后让child = DocumentField(CommonParent)
  • Write a custom Field that overrides DocumentField , but changes the validator to search through a list of types, instead of one. 编写一个重写DocumentField的自定义Field ,但更改验证器以搜索类型列表,而不是一个。
  • Just use an 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.

相关问题 在MongoAlchemy的索引中设置TTL - Settings TTL in MongoAlchemy's Index Argparser 的一个子解析器,用于两个不同的命令 - Argparser's one subparser for two different commands 我试图让 Selenium 在两个不同的文本片段上使用 XPath 单击一个对象,但出现错误 - I'm trying to get Selenium to click on one object using XPath on two different pieces of text, but there's an error 这两个简单的python代码有什么区别? (一个有效,另一个无效) - what's different between these two simple python codes? (one works and the other doesn't work) UML:如果两个不同类的对象都不包含另一个对象,它们是否可以具有聚合关系? - UML: Can two objects of different classes have an aggregation relationship if neither of them contain's the other one? MongoAlchemy ObjectIdField分辨率 - MongoAlchemy ObjectIdField resolution 如何使用两个不同的用户将文件从一个 S3 存储桶传输到另一个存储桶 - How to transfer a file from one S3 bucket to other with two different users 合并两个具有不同索引的数据帧,同时使用一行代码保留主数据帧的索引 - Merge two dataframes with different indices while preserving the main dataframe's index using a one-line code 验证MongoAlchemy中的唯一性? - Validating for uniqueness in MongoAlchemy? MongoAlchemy正则表达式不返回任何内容 - MongoAlchemy regex return nothing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM