我的目的是在管理站点上只查看用户名、电子邮件和电话号码。
我通过扩展 User 模型创建了 UserProfile:
模型.py
from django.db import models
from django.contrib.auth.models import User
class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
name = models.CharField(max_length=50, null=True,blank=True)
address = models.CharField(max_length=50, null=True,blank=True)
phone = models.CharField(max_length=20, null=True,blank=True)
country = models.CharField(max_length=20, null=True,blank=True)
state = models.CharField(max_length=20, null=True,blank=True)
zip = models.CharField(max_length=10, null=True,blank=True)
code = models.CharField(max_length=40, null=True)
def user_email(self):
return self.user.email
管理员.py
from myApp.models import UserProfile
from django.contrib import admin
class UserProfileAdmin(admin.ModelAdmin):
fields = ('name','phone',)
list_display = ('name','user_email',)
admin.site.register(UserProfile, UserProfileAdmin)
所以在 list_display 上它可以工作,我只能看到我选择的列,但是当我添加 'user_email' (fields = ('name','user_email', 'phone', ) )到我尝试时得到的字段时去管理网站:
“UserProfileAdmin.fields”指的是表单中缺少的字段“user_email”。