本文共 2140 字,大约阅读时间需要 7 分钟。
from django.db import models from django.contrib.auth.models import User #导入admin的User from django.db.models.signals import post_save # Create your models here. class UserProfile(models.Model): user = models.OneToOneField(User) description = models.TextField(max_length=51200) scope = models.IntegerField(default=100)admin.py
#coding:utf-8 from django.contrib import admin from django.contrib.auth.models import User from login.models import UserProfile # Register your models here. class ProfileInline(admin.StackedInline): #将UserProfile加入到Admin的user表中 model = UserProfile verbose_name = 'profile' class UserProfileAdmin(admin.ModelAdmin): inlines = (ProfileInline,) admin.site.unregister(User) #去掉在admin中的注册 admin.site.register(User, UserProfileAdmin) #用userProfileAdmin注册userviews.py
from django.shortcuts import render from django.http import HttpResponse from django.contrib.auth.models import User from login.models import UserProfile # Create your views here. def userDemo(request): desc = User.objects.all()[0] return HttpResponse(desc)
from django.conf.urls import include, url from django.contrib import admin urlpatterns = [ url(r'^admin/', include(admin.site.urls)), url(r'^login/', include('login.urls')) ]
from django.conf.urls import include, url from django.contrib import admin urlpatterns = [ url(r'^login/$', 'login.views.userDemo') ]
AUTH_USER_MODEL = 'login.User'login中:
from django.db import models from django.contrib.auth.models import AbstractUser # Create your models here. class User(AbstractUser): #继承AbstractUser desc = models.TextField()admin.py
#coding:utf-8 from django.contrib import admin # Register your models here. from login.models import User admin.site.register(User)其余的URL配置 ,views.py配置和settings.py的部分配置同profile的配置方式
转载地址:http://zgwvx.baihongyu.com/