简体   繁体   English

两个不同 Django 模型的外键

[英]Foreign Key to two different Django Models

For my CRM I am trying to create an History model, that should store every change or action made on a specific Ticket (aka Customer Request).对于我的 CRM,我正在尝试创建一个历史模型,该模型应该存储对特定票证(又名客户请求)所做的每个更改或操作。 In this scenario two type of users are involved: Resources and Customers.在这种情况下,涉及两种类型的用户:资源和客户。

Since both can make an action that should be stored in the History table (ex. Customers can open the ticket and Resources can close it) I was wondering what is the right way to achieve this as a Foreign Key ( actor )由于两者都可以执行应存储在历史记录表中的操作(例如,客户可以打开票证,资源可以关闭它),我想知道将其作为外键( actor )实现的正确方法是什么

class TicketHistory(models.Model):
    ticket = models.ForeignKey("Tickets", on_delete=models.PROTECT, related_name="ticket")
    action = models.ForeignKey("HistoryActions", on_delete=models.PROTECT, related_name="action")
    resourceActor = models.ForeignKey("Resources", ...)
    customerActor = models.ForeignKey("CustomerUsers" ...)
    date = models.DateTimeField(auto_now_add=True)

As you can see I've initially decided to create two different fields, but I don't think it is the best solution.如您所见,我最初决定创建两个不同的字段,但我认为这不是最佳解决方案。 How would you do this?你会怎么做?

I'd just use: https://django-simple-history.readthedocs.io/en/latest/我只是使用: https : //django-simple-history.readthedocs.io/en/latest/

Simple app - tracks all changes on an app/model简单的应用程序 - 跟踪应用程序/模型上的所有更改

You can use the admin site model log answered here , or you can use django-simple-history .您可以使用此处回答的管理站点模型日志,也可以使用django-simple-history This last one make the same that admin site history log.最后一个与管理站点历史记录相同。 Choose the one you like.选择你喜欢的那个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM