I have the following code in my view
page = get_object_or_404(Page, site = site, slug = slug)
pagecontents = PageContent.objects.filter(page=page)
pagecontents[0].description = form.cleaned_data['description']
pagecontents[0].save()
print pagecontents[0].description
print form.cleaned_data['description']
When the two print statements execute, i get the following
for the line print pagecontents[0].description
<p>Most important page</p>
for the line form.cleaned_data['description']
<p>Least important page</p>
Why is the object not getting saved?
You’re not keeping the changed object.
This makes an object from
pagecontents[0], changesdescription, and then loses track of the object, which gets garbage collected at some point.This makes another object from
pagecontents[0]and saves it, which does nothing since it is unchanged.pagecontentsis a queryset, so it’s not going to create any objects until it actually runs the query, which in this case is when you apply[0]to it.You need to keep track of the object: