a = Article(title="New Article", slug="new-article")
a.save()
print a
輸出
New Article
我如何僅title使用 force_update 進行更新。
uj5u.com熱心網友回復:
你可以試試這個方法:
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
uj5u.com熱心網友回復:
模型.py:
class Article(models.Model):
title = models.CharField(max_length=50)
slugfield ...
視圖.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
uj5u.com熱心網友回復:
如果你想用給定的 slug 更新記錄,你可以這樣做:
Article.objects.filter(slug='new-article').update(title='New Article')
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/340990.html
標籤:姜戈 django-views 定制 插入更新
上一篇:如何從非英語值創建slug?
下一篇:Django未在開發中渲染影像
