简体   繁体   中英

Django: How to modify the value of a Model attribute

Say I have a Django Person model connected to a sqlite3 database:

 class Person(models.Model): name = models.CharField(max_length=128) def __unicode__(self): return self.name 

Then I create an instance

 person = Person.objects.get_or_create(name="Henry")[0] 

How can I now change the name of Henry ? I tried

Person.objects.get(pk=1).name = "Alfred"

(where pk=1 corresponds to the primary key (?) of Henry ) but the name remains Henry , as I can see in my Django Admin.

You have to save your change :

person = Person.objects.get(pk=1)
person.name = "Alfred"
person.save()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM