Django 中的双外键?
有没有办法在 Django 中模拟双外键?
例如,如果我有表格:音频、覆盖、html 以及表:timeline_item,它有一个字段 id,以及一个指定音频、覆盖或 html 的字段类别...
有谁知道我将如何在 Django 中对此进行建模?或者是否有可能?
Is there anyway to model double foreign keys in Django?
For instance if I had tables: audio, overlay, html
and the table: timeline_item which has a field id, and a field category which specifies audio, overlay, or html...
Does anyone know how I would go about modeling this in Django? or if it's even possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来像是多态关联。也许您可以使用 ContentTypes< 来解决 Django 通用关系的问题/a> 框架。
Sounds like a polymorphic association. Maybe you can solve your problem with Django's generic relations using the ContentTypes framework.
外键是两个表之间的引用约束,因此您实际上不能让一列引用3个不同表上的3列。
请参阅:http://en.wikipedia.org/wiki/Foreign_key
你冷酷地让它有所不同,我相信代码最好能够演示:
然后您可以迭代项目并执行如下操作:
Foreign key is referential constraint between TWO tables so you really can't have one column referencing to 3 columns on 3 different tables.
see: http://en.wikipedia.org/wiki/Foreign_key
You cold make it somehow different, I believe code would be best to demonstrate:
then you can iterate over items and do something like this:
我最终做的是使用这个:
http://docs.djangoproject.com/en/1.0/topics/ db/models/#id7
在此之前,我使用了类中定义的另一种方法,该方法需要 2 个外键,仅将字段字典映射到其类并返回该对象类型。
What I ended up doing was using this:
http://docs.djangoproject.com/en/1.0/topics/db/models/#id7
Before that I was using another method defined in the class that needed 2 foreign keys that just mapped a dictionary of fields to their classes and returned that object type.