扩展方式1:profile扩展(外键方式)
在blog/models.py
from django.db import models
from django.contrib.auth.models import User #new
# Create your models here.
class UserProfile(models.Model):#new
user = models.OneToOneField(User)
desc = models.TextField(blank=True, null=True)
修改 blog/admin.py
from django.contrib import admin
from .models import UserProfile#
from django.contrib.auth.models import User#
# Register your models here.
class ProfileInline(admin.StackedInline)#
model = UserProfile
verbose_name = 'profile'
class UserAdmin(admin.ModelAdmin):#
inlines = [ProfileInline]
admin.site.unregister(User)#
admin.site.register(User, UserAdmin)#
manage.py
$createsuperuser
$ makemigrations
$migrate
$runserver
登陆/admin 完成
扩展方式2:继承AbstractUser
注意要先修改数据库名字 避免冲突
先在mysql中建个新库叫netdatadb2
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
# 'NAME': os.path.join(BASE_DIR,'db.sqlite3'),
#'NAME':'netDataDB',
'NAME': 'netDataDB2',
'USER':'root',
'PASSWORD':'953876',
'HOST':'localhost',
'PORT':'3306',
}
}
去掉第一种方法在各处的修改后
在blog/models.py中 修改
from django.db import models
from django.contrib.auth.models importAbstractUser#
# Create your models here.
class User(AbstractUser):#
desc =models.TextField()
在netdatablog/settings.py中添加
AUTH_USER_MODEL = 'blog.User'
注意可能需要 删除之前的migration文件 重新makemigration 和 migratecreatesuperuser
修改blog/admin.py
from django.contrib import admin
from .models import User#
# Register your models here.
admin.site.register(User)#
结束