我有一个继承自预定义 django 用户模型的交互模型,但我将 messageId 作为主键。
模型.py:
class Interaction(models.Model):
messageId = models.TextField(max_length=5000,primary_key=True, unique = True)
user = models.OneToOneField(User, primary_key = False, on_delete=models.CASCADE)
reciever = models.TextField(max_length=5000,blank=True,default='')
sender = models.TextField(max_length=5000,blank=True,default='')
date = models.TextField(max_length=5000,blank=True,default='')
subject = models.TextField(max_length=5000,blank=True,default='')
label = models.TextField(max_length=5000,blank=True,default='')
我有一个名为 data 的字典,它存储所需的信息。在views.py
entry, created = Interaction.objects.get_or_create(messageId=data['id'])
entry.user = request.user
entry.sender = data['from']
entry.reciever = data['to']
entry.date = data['date']
entry.subject = data['subject']
entry.label = data['label']
entry.save()
因此,在运行代码时,有相同用户的消息,但消息 ID 明显不同
用户不是我的主键,但我不断收到错误
djongo.sql2mongo.SQLDecodeError: FAILED SQL: UPDATE "mainapp_interaction" SET "user_id" = %(0)s, "reciever" = %(1)s, "sender" = %(2)s, "date" = %(3)s, "subject" = %(4)s, "label" = %(5)s WHERE "mainapp_interaction"."messageId" = %(6)s
Pymongo error: {'index': 0, 'code': 11000, 'errmsg': 'E11000 duplicate key error collection: 5d02255b9ccf640b8c608ee1_mysitev6.mainapp_interaction index: user_id_1 dup key: { : 16 }'}