创建两个模型的反向关系
我有一个模型User
。出于性能和其他原因,我必须将此模型及其表分成两部分:UserA
和 UserB
。我决定使用物化视图(在 django-pgviews 的帮助下)。现在很简单的是查询表中已有的 UserA
数据,例如用户名或密码。行不通的是反向关系。假设我有一个 Checkout
模型,其中:
user = models.ForeignKey(User, on_delete=models.SET_NULL, related_name='checkouts')
当我尝试访问 user.checkouts
时,它当然会抛出一个错误:无法解析关键字“checkouts”进入字段
。
那么如何创建可从多个模型访问的反向关系呢?我考虑过使用 contenttypes 但这似乎有点太多了用例,特别是因为 UserA 和 UserB 只是视图并且具有相同的列。
I have a model User
. For performance and other reasons I have to split this Model and its table into two, UserA
and UserB
. I decided to use materialized views (with the help of django-pgviews). Now what IS easy is to query the data of UserA
which is already in the table, like the username or password. What doesn't work are reverse relationships. Let's say I have a Checkout
model and in this:
user = models.ForeignKey(User, on_delete=models.SET_NULL, related_name='checkouts')
When I try to access user.checkouts
, it of course throws an error: Cannot resolve keyword 'checkouts' into field
.
So how can I create a reverse relationship which is accessible from multiple models? I thought about using contenttypes but this seems a bit much for this use case, especially because UserA and UserB are just views and have the same columns.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论