A
A
Artur Kudashev2019-01-22 18:35:41
Django
Artur Kudashev, 2019-01-22 18:35:41

How to change only one field in the database?

Good day. I want to make a view counter, but there was a problem.

def post(request, pk):
  post = Post.objects.get(pk = pk)
  post.count += 1
  post.save()

  return render(request, 'myblog/single.html', {'post': post})

The counter works, but when a new object is saved, the field with the publication date is changed (auto_now_add = True). Is it possible to change only the view field? If there are other options I will be glad. Thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kitaev, 2019-01-22
@deliro

post.save(update_fields=["count"]), but better as Sergei Tikhonov said

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question