且构网

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

保存前如何更改Django表单字段值?

更新时间:2023-10-05 23:07:58

如果在保存之前需要对数据做一些事情,只需创建一个函数,如:

If you need to do something to the data before saving, just create a function like:

def clean_nameofdata(self):
    data = self.cleaned_data['nameofdata']
    # do some stuff
    return data

所有您需要创建的名称为** clean _ *** nameofdata *的函数,其中 nameofdata 是字段的名称,因此,如果要修改密码字段,则需要:

All you need is to create a function with the name **clean_***nameofdata* where nameofdata is the name of the field, so if you want to modify password field, you need:

def clean_password(self):

如果您需要修改 passwordrepeat

def clean_passwordrepeat(self):

因此在其中,只需对您的密码进行加密并返回加密的密码即可.

So inside there, just encrypt your password and return the encrypted one.

我的意思是:

def clean_password(self):
    data = self.cleaned_data['password']
    # encrypt stuff
    return data

因此,当您确认表格有效时,密码将被加密.

so when you valid the form, the password would be encrypted.