SQLAlchemy通用关系简单示例 [英] SQLAlchemy Generic Relationship simple example

查看:50
本文介绍了SQLAlchemy通用关系简单示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有人问过类似的问题,但是我真的很难理解SQLAlchemy中如何实现通用字段.

I know similar questions have been asked, however I am really struggling to understand how generic fields are implemented in SQLAlchemy.

我有一个Permissions类/表,我想包含一个可以与任何模型类型相关的字段.

I have a Permissions class/table which I want to contain a field which can relate to any model type.

我已经查看了示例,并且此博客文章 http://techspot.zzzeek.org/2007/05/29/polymorphic-associations-with-sqlalchemy/

I have looked at the examples and this blog post http://techspot.zzzeek.org/2007/05/29/polymorphic-associations-with-sqlalchemy/

是否可以在没有单独表的情况下具有通用关系?仅通过存储object_type和id?遵循以下原则:

Is it possible to have a generic relation without a separate table? Just by storing the object_type and id? Something along these lines:

class Permission(AbstractBase):
    user = relationship("User", backref=backref('permissions'))
    permission_type = column(String())
    object = #The object the permission applies to, could be any type.

我想只是一个非常简单的例子就可以了!

I guess just a really simple example would be appreciated!

另外,值得注意的是我来自Django背景!

Also, it's worth noting I am coming from a Django background!

谢谢

推荐答案

大约一天后,有人问了同样的问题,我指出了我们为这类事情准备的三个例子,但我也写了一个新的例子(大多数情况下)执行Django的相同操作(减去奇怪的"contenttypes"表):

Someone else asked the identical question about a day later, I pointed to the three examples we have for this kind of thing but also I wrote a new example that will do (mostly) the same thing Django does (minus that weird "contenttypes" table): sqlalchemy generic foreign key (like in django ORM)

这篇关于SQLAlchemy通用关系简单示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆