Asif Akhtar
Asif Akhtar

Reputation: 333

ValueError: save() prohibited to prevent data loss due to unsaved related object 'sender'

I am trying to create Message obj while creating an object for Quotation. i've tried looking at the official Django docs, but it doesn't seem to apply. I don't know where is the error.

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)

and here is my Quotation Table

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)

and here is my view function:

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
)

Upvotes: 2

Views: 6909

Answers (1)

Apoorv Kansal
Apoorv Kansal

Reputation: 3290

When you use, Quotation.objects.create, Django will automatically create AND save the object to the database. Hence, quotation.save() should be removed as it's unnecessary.

On top of this, using User(...) will NOT save the object into the database. You still need to call save OR use the above mentioned solution (create) before inserting the users for sender and receiver foreign keys.

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
)

Upvotes: 5

Related Questions