且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Django/Python更新字段值(在模型保存期间)

更新时间:2023-12-01 13:59:28

您可以使用setattr(self,my_field_name,value)来实现.

You can use setattr(self, my_field_name, value) to achieve this.

for field in ['first_name', 'last_name']: 
    value = getattr(self, field)
    setattr(self, field, value.title())

由于字符串在Python中是不可变的,因此您将无法就地修改值.

You won't be able to modify the value in-place as strings are immutables in Python.