Как использовать force_update в Django?

a = Article(title="New Article", slug="new-article")
a.save()
print a

OUTPUT

New Article

Как мне обновить только title используя force_update.

Вы можете попробовать следующий способ:

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

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

Если вы хотите обновить запись с заданным slug, вы можете сделать это с помощью:

Article.objects.filter(slug='new-article').update(title='New Article')
Вернуться на верх