Django 为多个表建模一个外键 [英] Django model one foreign key to many tables

查看:27
本文介绍了Django 为多个表建模一个外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个问题,我想创建一个具有指向其他几个表的外键的单个表,并使用另一个字段类型"来说明该键应该属于哪个表.

So I have a question I was thinking of creating a single table that has a foreign key to several other tables, and using another field "type" to say what table the key should belong to.

class Status(Models.model):
    request = models.ForeignKey("Request1", "Request2", "Request3")
    request_type = models.IntegerField()
    ...Some status related data

class Request1(Models.model):
    ...Some data

class Request2(Models.model):
    ...Some other data

Class Request3(Models.model):
   ...different data

我的问题是,是否可以像这样定义外键?我想到的另一个解决方案是像这样定义我的模型

My question is, is it possible to define a foreign key like this? another solution I thought of was to define my model like this

class Status(Models.model):
    request1 = models.ForeignKey("Request1")
    request2 = models.ForeignKey("Request2")
    request3 = models.ForeignKey("Request3")
    ...Some status related data

class Request1(Models.model):
    ...Some data

class Request2(Models.model):
    ...Some other data

Class Request3(Models.model):
   ...different data

但是,如果我这样做,是否可以通过 django 定义一个约束,表示只允许 1 个外键具有数据,而其他两个必须为空?或者我是否必须在 db 端严格设置这个约束.(我正在使用 postgres)我希望能够在创建 db 时告诉 django 这样做,这样我就不必每次都记住重新创建数据库.

But if I do it this way is it possible to define a constraint via django that says only 1 foreign key is allowed to have data and the other two must be null? or will I have to strictly set this constraint up on the db side.(I'm using postgres) I would like to be able to tell django to do it when it creates the db so I don't have to remember every time someone recreates the db.

任何意见或建议将不胜感激.我不认同这些想法中的任何一个,所以如果有另一种聪明的方法来达到同样的效果,我很乐意听到.感谢您的时间.

Any input or advice would be greatly appreciated. I am not married to either of these ideas, so if there is another clever way to achieve the same effect i am up to hear it. Thank you for your time.

我使用的是 django 1.7.10

I am using django 1.7.10

推荐答案

你应该使用 Django 中的 contenttypes 框架.

You should use the contentypes framework in Django.

这里有一个泛型关系的例子:https://docs.djangoproject.com/en/1.8/ref/contrib/contenttypes/#generic-relations根据您的要求,它可能如下所示:

There's an example for a generic relation here :https://docs.djangoproject.com/en/1.8/ref/contrib/contenttypes/#generic-relations For your requirement it could look something like this:

from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType

class Status(models.Model):
    request_type = models.ForeignKey(ContentType)
    request_id = models.PositiveIntegerField()
    request = GenericForeignKey('request_type', 'request_id')

然后您可以执行以下操作:

You can then do something like following:

status1 = Status(request=Request1("foo"))
status1.save()
status2 = Status(request=Request2("bar"))
status2.save()

status1.request // <Request1 "foo">
status2.request // <Request2 "bar">

这篇关于Django 为多个表建模一个外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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