【发布时间】:2016-12-10 04:01:19
【问题描述】:
我正在尝试在创建报价对象时创建消息 obj。我试过查看官方的 Django 文档,但它似乎并不适用。不知道哪里出错了。
class Message(models.Model):
sender = models.ForeignKey(User, related_name="sender_user")
recipient = models.ForeignKey(User, related_name="recipient_user")
sender_read = models.BooleanField(default=False)
recipient_read = models.BooleanField(default=False)
parent_msg = models.ForeignKey("self", null=True, blank=True, related_name="parent")
subject = models.CharField(max_length=255, blank=True, null=True)
message = models.TextField(null=True, blank=True)
created = models.DateTimeField(auto_now_add=True, null=True, blank=True)
这是我的报价表
class Quotation(models.Model):
editor = models.ForeignKey(Editors)
discipline = models.ForeignKey(Discipline, null=True, blank=True)
title = models.CharField(max_length=255)
description = models.TextField(null=True, blank=True)
words_count = models.CharField(max_length=255)
student = models.ForeignKey(Students, null=True, blank=True)
created = models.DateTimeField(auto_now_add=True, null=True, blank=True)
modified = models.DateTimeField(auto_now=True, null=True, blank=True)
这是我的视图函数:
quotation = Quotation.objects.create(
editor=editor,
student=student,
title=form.cleaned_data['title'],
discipline = discipline,
description = form.cleaned_data['description'],
words_count=form.cleaned_data['words_count']
)
quotation.save()
create_message = Message.objects.create(
sender= User(username=quotation.student.user.username),
recipient=User(username=quotation.editor.user.username),
subject=quotation.title,
messages=quotation.description
)
【问题讨论】:
标签: django django-models