无法将正确的对象实例分配给ForeignKey字段

时间:2015-09-07 09:50:15

标签: django socket.io

我想保存用户在我的Django Socket.IO聊天应用程序中发送的消息,并为此提供以下模型:

class Conversation(models.Model):
    """
    Conversation with two or more participants.
    """
    name = models.CharField(max_length=20)
    ...

class Message(models.Model):
    """
    Single chat message.
    """
    conversation = models.ForeignKey(Conversation,related_name='message_convo')
    content = models.CharField(max_length=10000)
    sender = models.ForeignKey(User,related_name='message_sender')
    sent = models.DateTimeField(auto_now=True)

我的事件处理程序代码包括以下内容:

convo = get_object_or_404(Conversation, id=message["room"])
...
m = Message()
m.conversation = convo,
m.content = message["message"]
m.sender = user
m.save()

这失败了一条完全没有意义的消息: ValueError: Cannot assign "(<Conversation: ImmaMode, Century>,)": "Message.conversation" must be a "Conversation" instance.

由于convo返回get_object_or_404并且明确一个Conversation实例,可能导致此问题的原因是什么?!

1 个答案:

答案 0 :(得分:2)

m.conversation = convo,

逗号?