a = Article(title="New Article", slug="new-article")
a.save()
print a
OUTPUT
New Article
How I update only title
using force_update.
CodePudding user response:
You can try this way:
a = Article(title="New Article")
a.save(force_insert=True)
print a
New Article
a.title = "Better Title"
a.save(force_update=True)
print a
CodePudding user response:
Models.py :
class Article(models.Model):
title = models.CharField(max_length=50)
slugfield ...
Views.py:
#Create a model instance
a = Article(title="New Article", slug="new-article")
a.save()
#Article object created
#To update an existing article object first select that object by,
article_to_be_updated = Article.objects.get(id = #article's id)
#now update the selected articles field and then save it by,
article_to_be_updated.title = "updated title"
article_to_be_updated.save()
#updated only the title and reflected changes to DB
CodePudding user response:
If you want to update the record with the given slug, you can do this with:
Article.objects.filter(slug='new-article').update(title='New Article')