django 中单独执行py文件修改用户名
Python文件代码
import os import django # 在environ字典里设置默认Django环境,'xxxx.settings'指Django项目的配置文件 os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'blog_demo.settings')# environ是字典,同os.environ['DJANGO_SETTINGS_MODULE'] django.setup() from blog.models import UserInfo u = UserInfo.objects.get(username='yuan') u.set_password('yuan123') u.save()
UserInfo:
from django.db import models # Create your models here. from django.contrib.auth.models import AbstractUser class UserInfo(AbstractUser): """ 用户信息 """ nid = models.AutoField(primary_key=True) telephone = models.CharField(max_length=11, null=True, unique=True) avatar = models.FileField(upload_to='avatars/', default="/avatars/default.png") create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True) blog = models.OneToOneField(to='Blog', to_field='nid', null=True, on_delete=models.CASCADE) def __str__(self): return self.username
然后单独Run运行此py文件
(0.001) SELECT @@SQL_AUTO_IS_NULL; args=None (0.000) SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED; args=None (0.001) SELECT `blog_userinfo`.`password`, `blog_userinfo`.`last_login`, `blog_userinfo`.`is_superuser`, `blog_userinfo`.`username`, `blog_userinfo`.`first_name`, `blog_userinfo`.`last_name`, `blog_userinfo`.`email`, `blog_userinfo`.`is_staff`, `blog_userinfo`.`is_active`, `blog_userinfo`.`date_joined`, `blog_userinfo`.`nid`, `blog_userinfo`.`telephone`, `blog_userinfo`.`avatar`, `blog_userinfo`.`create_time`, `blog_userinfo`.`blog_id` FROM `blog_userinfo` WHERE `blog_userinfo`.`username` = 'yuan'; args=('yuan',) (0.000) UPDATE `blog_userinfo` SET `password` = 'pbkdf2_sha256$120000$OVbC4g3x30yE$uowadfulAlTp8K1UG5BApAQ8zuTVNjefMXfCm53BGMQ=', `last_login` = '2019-03-19 16:08:12', `is_superuser` = 1, `username` = 'yuan', `first_name` = '', `last_name` = '', `email` = 'yuan@163.com', `is_staff` = 1, `is_active` = 1, `date_joined` = '2019-03-18 15:23:14', `telephone` = '18910121212', `avatar` = 'avatars/namei.jpg', `create_time` = '2019-03-18 15:23:14.164201', `blog_id` = 2 WHERE `blog_userinfo`.`nid` = 1; args=('pbkdf2_sha256$120000$OVbC4g3x30yE$uowadfulAlTp8K1UG5BApAQ8zuTVNjefMXfCm53BGMQ=', '2019-03-19 16:08:12', True, 'yuan', '', '', 'yuan@163.com', True, True, '2019-03-18 15:23:14', '18910121212', 'avatars/namei.jpg', '2019-03-18 15:23:14.164201', 2, 1)