django 擴充套件user使用者欄位inlines
阿新 • • 發佈:2018-11-26
model:
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, verbose_name='暱稱')
nickname = models.CharField(max_length=20)
def __str__(self):
return '<Profile: %s for %s>' % (self.nickname, self.user.username)
admin.py
from django.contrib import admin from django.contrib.auth.models import User from .models import Profile class ProfileInline(admin.StackedInline): #將Profile加入到Admin的user表中,內聯 model = Profile can_delete = False class UserAdmin(admin.ModelAdmin): inlines = [ProfileInline] list_display = ('username', 'nickname', 'email', 'is_staff', 'is_active', 'is_superuser') def nickname(self, obj): return obj.profile.nickname #自定義欄位顯示資訊 nickname.short_description = '暱稱' # Re-register UserAdmin admin.site.unregister(User) #去掉在admin中的註冊 admin.site.register(User, UserAdmin) #用UserAdmin註冊user