Django之form重寫save()方法
阿新 • • 發佈:2019-05-08
nco hashlib hex 自動 elf orm ges char inf
class UserInfo(models.Model): username = models.CharField("用戶名", max_length=64, unique=True) password = models.CharField("密碼", max_length=64) uid = models.CharField(verbose_name=‘個人唯一ID‘, max_length=64, unique=True) wx_id = models.CharField(verbose_name="微信ID", max_length=128, blank=True, null=True, db_index=True) def save(self, *args, **kwargs): # 創建用戶時,為用戶自動生成個人唯一ID if not self.pk: # 存在就更新,不存在就創建 m = hashlib.md5() m.update(self.username.encode(encoding="utf-8")) self.uid = m.hexdigest() super(UserInfo, self).save(*args, **kwargs)
Django之form重寫save()方法