所以我遇到了一些奇怪的事情。我只是注意到我的管理页面上显示的所有字段都不存在。失败的模型是一个“Profile”模型,它是User模型的扩展,具有oneToOne关系。代码看起来像这样。
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
# Create your models here.
#We need to extend the User Model
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
bio = models.CharField(max_length=4096, null=True)
email_confirmed = models.BooleanField(default=False)
def __str__(self):
return self.user.username
def get_username(self):
return self.user.username
def get_bio(self):
return self.bio
def get_email_confirmed(self):
return self.email_confirmed
@receiver(post_save, sender=User)
def update_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
instance.profile.save()
from django.contrib import admin
from .models import Profile
# Register your models here.
class ProfileAdmin(admin.ModelAdmin):
list_display = ('email_confirmed', 'bio')
admin.site.register(Profile)
但是,即使进行了这些更改,管理页面也会如下所示:
答案 0 :(得分:1)
我还没有完全通过,因为我现在没有工作的Django环境。试试吧。我有一个类似的代码。可能是它。
class ProfileAdmin(admin.ModelAdmin):
list_display = ('email_confirmed', 'bio')
admin.site.register(Profile,ProfileAdmin)
答案 1 :(得分:1)
您的admin.py应如下所示:
from django.contrib import admin
from .models import Profile
# Register your models here.
class ProfileAdmin(admin.ModelAdmin):
list_display = ('email_confirmed', 'bio')
admin.site.register(Profile, ProfileAdmin)
最后一个方法应该注册模型和管理类。