【问题标题】:ValueError: save() prohibited to prevent data loss due to unsaved related object 'sender'ValueError: save() 禁止以防止由于未保存的相关对象“发件人”而导致数据丢失
【发布时间】: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


    【解决方案1】:

    当你使用 Quotation.objects.create 时,Django 会自动创建并将对象保存到数据库中。因此,quotation.save() 应该被删除,因为它是不必要的。

    除此之外,使用User(...) 不会将对象保存到数据库中。在插入senderreceiver 外键的用户之前,您仍然需要调用save 或使用上述解决方案(create)。

    views.py:

    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']
    )
    
    sender = User.objects.create(username=quotation.student.user.username)
    receiver = User.objects.create(username=quotation.editor.user.username)
    create_message = Message.objects.create(
        sender= sender,
        recipient=receiver,
        subject=quotation.title,
        messages=quotation.description
    )
    

    【讨论】:

    • 是的,我已经删除了它,但仍然遇到同样的错误
    • 不,你还没有看到我的编辑。我改变了你创建 Message 对象的方式。
    • quotation = Quotation.objects.create( editor=editor, student=student, title=form.cleaned_data['title'], 学科 = 学科, description = form.cleaned_data['description'], words_count=form.cleaned_data['words_count']) create_message = Message.objects.create(sender= User(username=quotation.student.user.username),receiver=User(username=quotation.editor.user.username),主题=quotation.title, message=quotation.description)
    • 是的,谢谢,该错误已被删除,但出现了新错误。
    • 唯一约束失败:auth_user.username
    猜你喜欢
    • 2016-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多