I have a Message model, And in it I have create_user
ForeignKey and receive_user
ManyToManyField:
class Message(models.Model):
"""
消息
"""
message_num = models.CharField(default=getMessageNum, max_length=16, help_text="消息") # 注意:message_num 相同,说明是同一次发送
title = models.CharField(max_length=64, help_text="消息名称")
content = models.CharField(max_length=1024, help_text="消息内容")
create_user = models.ForeignKey(User, related_name="created_messages",help_text="创建者")
receive_user = models.ManyToManyField(User, related_name="received_messages", help_text="接受者")
def __str__(self):
return self.title
def __unicode__(self):
return self.title
When I use the bellow to save a message, I except a exception:
try:
receive_user = User.objects.get(id=user_id)
message = Message.objects.create(
title=title,
content=content,
create_user=create_user,
receive_user=receive_user,
)
message.save()
except Exception as e:
raise e
I get the exception:
"<Message: title>" needs to have a value for field "id" before this many-to-many relationship can be used.
How to resolve this issue? some friend can help me about this?