Suppose this view that is for edit record:
def edit_post(request, slug):
post = get_object_or_404(Post, slug=slug)
if request.method == "POST":
form = AddPostForm(request.POST, request.FILES, instance=post)
# 1
if form.is_valid():
# 2
new_post = form.save(commit=False)
new_post.save()
return redirect('administrator:view_admin_post')
...
Now assume these:
I have
field1
that is exist inPOST
model.field1
has default value and suppose that the current value offield1
depends on previous.- Also suppose that I don't want to pass the
field1
to user. As a result, I will not have it inrequest.POST
.
In this situation when I write print(post.field1)
In line # 1
I have previous value but when I print that in
line # 2
I got None
!
What is going on?And how can I have my post.field1
?
Notice: I know I can define a middleware variable and save the previous value in this variable.But is there a better way to do this?