'Cannot add "<Model: Model object (None)>": the value for field "field" is None
I am trying to save an object to my database while adding it to the Many to Many field of another object. I already tried many other solutions from here but nothing worked so far.
Model:
class SellerPost(models.Model):
post_uuid = models.UUIDField(default=uuid.uuid4, editable=False)
seller = models.ForeignKey("User", on_delete=models.CASCADE)
text_content = models.TextField()
comments = models.ManyToManyField("SellerPostComment", blank=True)
class SellerPostComment(models.Model):
comment_id = models.IntegerField(primary_key=True)
post = models.ForeignKey(SellerPost, on_delete=models.CASCADE)
addressed = models.ForeignKey("User", on_delete=models.CASCADE, null=False, related_name="seller_addressed_comment")
commenter = models.ForeignKey("User", on_delete=models.CASCADE, null=False)
content = models.TextField()
View (i cut everything but the essential part that has sth to do with the error):
post = request.POST["post"]
post_obj = SellerPost.objects.get(post_uuid=post)
comment = comment_form.save(commit=False)
comment.addressed = user
comment.commenter = request.user
comment.post = post_obj
comment.save()
post_obj.comments.add(comment)
return redirect(index)
class PostCommentForm(forms.ModelForm):
class Meta:
model = SellerPostComment
fields = ("content",)
def save(self, commit=True):
comment = super(PostCommentForm, self).save(commit=False)
if commit:
comment.save()
return comment
Error:
Cannot add "<SellerPostComment: SellerPostComment object (None)>": the value for field "sellerpostcomment" is None
The form is valid but it just won't save the comment to the M2M field of the post. Thanks in advance!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
