English 中文(简体)
如何在 Ming ODM 中映射自定义类型?
原标题:How to map a custom type in Ming ODM?

我正在在Ming ODM 中建立映射程序。 出现的一个问题是如何将自定义类型映射到字段,以及如何将自定义类型传送到 .query

基本上,我有一个可以代表客户身份的物件。 我喜欢 FieldProperty 来取回这种类型的物件, 而不是内建类型之一。 这种类型可以转换到或从内建类型中。 它将存储在 mongo 中, 但是在数据模型和其他 Python 代码中, 我想把它作为这个特定域的类型传过来 。

PyMongo抱怨说,当直接以 kwd 参数的形式将ClobalID 类型作为kwd 参数转至 < code>.query 时,PyMongo抱怨说,它不理解该类型。

问题回答

我最近想做类似的事情; 通过 MongoDB 将 IP 地址作为二进制数据存储在 MongoDB 中 。 (原因在于允许有效的 CIDR 查询 。 ) 我为在尝试同样的事情时碰巧遇到这个问题的其他人提供我的解决方案, 尽管帮助@kbluck可能为时已晚 。

我通过把IP地址类变成 bson.Binary (使用 Python 2 Im; 对于 Python 3, 将使用内建 binary 类型中的子类) 的伎俩来完成这项工作。

您会将您的 CustomerID 类作为 int 的子类。 CustomerID 类需要有一个接受 int (在从数据库装入时) 的多形态构建器,一个 CustomerID 例(复制构建器),以及任何需要使用的构建器(例如,我的IP地址演示字符串)。

这不是你所要求的; FieldProperty 要么使用 int ,要么使用您的类型,然后返回一个知道如何转换为您类型的特殊的 int

我上传了一个完整的例子(IP地址),作为主词:https://gist.github.com/3854792





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签