vjwvebjkwev
vjwvebjkwev

Reputation: 3

"Cannot force an update in save() with no primary key"- when submitting an edit/update form

I have an edit form that I only want to update the "title", "content", and "category" of a Post. To this end, I have included update_fields when saving the form. However, there's a wee issue with this. Django raises a ValueError (specifically "Cannot force an update in save() with no primary key") when I submit the form. Why is it doing this, and how can I fix this?

Post form:

class PostForm(forms.ModelForm):

    def __init__(self,  *args, **kwargs):
        super().__init__(*args, **kwargs)
        for key in ['postTitle', 'postContent', 'category']:
            self.fields[key].widget.attrs.update({'class': 'form-control'})         
        self.fields['content'].widget.attrs.update(width='100px', height='50')

    class Meta:
        model = Post
        fields = ('title', 'content', 'category') 

Post model:

class Post(models.Model):
    title = models.CharField(max_length = 100)
    URL = models.SlugField() # slug for url 
    content = models.TextField()
    author = models.ForeignKey(User, on_delete = models.CASCADE, related_name = 'posts') # id of author
    category = models.CharField(max_length = 9, default = 'General') # category the post is in
    creationDate = models.DateTimeField()

Edit view:

def editPost(request, pk):
    if request.method == 'GET':
        post = get_object_or_404(Post, pk = pk)
        if post.author == request.user:
            form = PostForm(instance = post)
            return render(request, 'editPost.html',  {'form': form, 'post': post})
        else:
            return redirect('viewPost', pk = pk, postURL = post.postURL)
    if request.method == 'POST':
        post = get_object_or_404(Post, pk = pk)
        form = PostForm(request.POST)
        if post.author == request.user:
            if form.is_valid():
                post = form.save(commit=False)
                post.URL = slugify(post.postTitle)
                post.save(update_fields = ['title', 'content', 'category', 'postURL'])
    return redirect('viewAll')

Upvotes: 0

Views: 4233

Answers (1)

Ozgur Akcali
Ozgur Akcali

Reputation: 5492

Seems like you are re-assigning post here. In this line;

post = get_object_or_404(Post, pk = pk)

You are getting a Post instance from the database. Then in this line;

post = form.save(commit=False)

You are re-assigning post variable to an unsaved instance of a Post, populated with only the data that is available in request.POST. Then, when you want to save this post with an update_fields argument like this:

post.save(update_fields = ['title', 'content', 'category', 'postURL'])

ValueError is raised because the Post instance you are trying to save does not have an id, it only has what is available in request.POST

Upvotes: 2

Related Questions