在django
定義,我們怎麼設置都沒有class Meta
定義字段`如何設置場模型,這不是在元類
我有這樣的模式:
class User(models.Model):
user_name = models.CharField(max_length=32, unique=True)
email = models.EmailField(unique=True)
first_name = models.CharField(max_length=32)
last_name = models.CharField(max_length=32)
password_hash = models.CharField(max_length=64)
password_salt = models.CharField(max_length=64)
這形式:
class ContactForm(ModelForm):
password = forms.CharField(widget=forms.PasswordInput())
repeat_password = forms.CharField(widget=forms.PasswordInput())
class Meta:
model = User
fields = ['user_name', 'email', 'first_name', 'last_name']
def clean(self):
cleaned_data = super(ContactForm, self).clean()
password = cleaned_data.get('password')
repeat_password = cleaned_data.get('repeat_password')
if password is not None and repeat_password is not None:
if password != repeat_password:
raise forms.ValidationError("Passwords do not match.")
password_validate = password_validate = password_validation.validate_password(password)
if password_validate is not None:
raise password_validate
我只希望用戶能夠設置user_name
,email
,first_name
和last_name
領域。在clean(self)
我檢查輸入的密碼是否相符,以及它們是否有效。
我現在想要做的是設置我的模型User
的password_hash
和password_salt
。如何才能做到這一點?
在點。謝謝@Fazil。 – arminb