Django 基于字段值链接到其他模型的模型

Django 基于字段值链接到其他模型的模型,django,django-models,Django,Django Models,我正在开发简单的评分和评论应用程序,以添加到我的项目中,并正在寻找关于创建模型的建议 通常,我会像这样创建这些数据库模式: comment_ id - primary key type - varchar (buyer_item, buyer_vendor, vendor_buyer) source_id - int (primary key of the table based on the type) target_id - int (primary key

我正在开发简单的评分和评论应用程序,以添加到我的项目中,并正在寻找关于创建模型的建议

通常,我会像这样创建这些数据库模式:

comment_
    id - primary key
    type - varchar (buyer_item, buyer_vendor, vendor_buyer)
    source_id - int (primary key of the table based on the type)
    target_id - int (primary key of the table based on the type))
    timestamp - timestamp
    subject - varchar
    comment - text


rating_
    id - primary key
    type - varchar (buyer_item, buyer_vendor, vendor_buyer)
    source_id - int (primary key of the table based on the type)
    target_id - int (primary key of the table based on the type)
    timestamp - timestamp
    rating - int (the score given, ie: 1-5 stars)
这将使我有一些简单的方法,允许我通过设置适当的类型并设置提交者的id(源id)以及它适用的id(目标id),对任何类型的事物应用评论或评级,例如:

我知道在模型中,您将与其他表的关系定义为模型的一部分。我如何定义这些类型的表中的关系,其中类型字段确定源ID和目标ID链接到哪个表

或者我应该从模型中省略关系,并在获取查询集时设置连接

或者干脆扔掉who通用表格的想法,为每个关系(例如:用户评分、产品评分、交易评分等)制作一大堆不同的表格

这里的最佳实践是什么?我的DBA感觉说使用普通表,但Django新手我不确定本地人做什么


谢谢

我认为您正在寻找的是一种泛型关系,您可以在contenttypes框架中找到这种类型的东西:

请显示您的模型。py我还没有编写它。这就是问题的重点,确定最佳的写作方式。:-)是的,看起来这就是我想要的功能类型。在我的例子中,不能对它们进行过滤或聚合的权衡可能会抵消它们的有用性。看起来我最好为事物之间的每种关系创建不同的模型,并提供附加的功能。
add_comment('user_product', user.pk, product.pk, now, subject, comment)
add_comment('user_vendor', user.pk, vendor.pk, now, subject, comment)