Django 管理员不会显示字段
Posted
技术标签:
【中文标题】Django 管理员不会显示字段【英文标题】:Django Admin Won't Show Fields 【发布时间】:2016-06-20 04:22:41 【问题描述】:在我的实时网站上的 Django Admin 中,编辑某些模型的表单是空白的。黄色栏下应该有字段:
由于某种原因,这只会影响两个模型(用户和作业),而其他模型工作正常。在我的本地测试服务器上测试网站时,受影响模型上的字段也会出现。这是我的 admin.py:
from django.contrib import admin
from web.models import Address, User, WorkerProfile, EmployerProfile, Job, SupportIssue, LoginCode, ReferredEmail, InArea, AdminEmail
from django.contrib.auth.models import Group
class AddressAdmin(admin.ModelAdmin):
search_fields = ('name', 'zip',)
class UserAdmin(admin.ModelAdmin):
search_fields = ('email', 'phone', 'role', 'address__zip',)
exclude = ('birthday', 'password', 'last_login', 'is_active',)
class WorkerProfileAdmin(admin.ModelAdmin):
search_fields = ('user__email', 'user__phone', 'user__address__zip',)
class EmployerProfileAdmin(admin.ModelAdmin):
search_fields = ('user__email', 'user__phone', 'user__address__zip',)
class JobAdmin(admin.ModelAdmin):
search_fields = ('employer__user__email', 'employer__user__phone', 'worker__user__email', 'worker__user__phone', 'title', 'address__zip',)
exclude = ('is_hired', 'is_completed', 'employer_is_reviewed',)
class SupportIssueAdmin(admin.ModelAdmin):
search_fields = ('email',)
class LoginCodeAdmin(admin.ModelAdmin):
search_fields = ('email',)
class ReferredEmailAdmin(admin.ModelAdmin):
search_fields = ('email', 'user__email',)
class InAreaAdmin(admin.ModelAdmin):
search_fields = ('email', 'state',)
class AdminEmailAdmin(admin.ModelAdmin):
search_fields = ('filter_value', 'subject', 'date',)
admin.site.register(Address, AddressAdmin)
admin.site.register(User, UserAdmin)
admin.site.register(WorkerProfile, WorkerProfileAdmin)
admin.site.register(EmployerProfile, EmployerProfileAdmin)
admin.site.register(Job, JobAdmin)
admin.site.register(SupportIssue, SupportIssueAdmin)
admin.site.register(LoginCode, LoginCodeAdmin)
admin.site.register(ReferredEmail, ReferredEmailAdmin)
admin.site.register(InArea, InAreaAdmin)
admin.site.register(AdminEmail, AdminEmailAdmin)
admin.site.unregister(Group)
我已经使用以下命令迁移了一台服务器:
python manage.py makemigrations APPNAME
python manage.py migrate
python manage.py syncdb
我还重新启动了我的服务器,并且 admin.py 文件是最新的。我正在使用 django 1.8.5、apache 2.4 和 mysql 有人知道问题的原因吗?有没有人遇到过这个问题或知道如何解决?
编辑(models.py):
class Address(models.Model):
state_choices = (
('OH', 'Ohio'),
)
name = models.CharField(max_length=255, verbose_name='Name')
street = models.CharField(max_length=255, verbose_name='Street Address')
city = models.CharField(max_length=31, verbose_name='City')
state = models.CharField(max_length=255, choices=state_choices, verbose_name='State')
zip = models.CharField(validators=[MinLengthValidator(5)], max_length=5, verbose_name='Zip Code')
def __str__ (self):
return self.name + ' ' + self.street + ' ' + self.city + ' ' + self.state + ' ' + self.zip
class UserManager(BaseUserManager):
def create_user(self, first_name, last_name, email, phone, role, birthday, address, password):
user = self.model(first_name=first_name, last_name=last_name, email=email, phone=phone, role=role, birthday=birthday, address=address)
user.set_password(password)
user.admin_permission = False
user.save()
return user
def create_superuser(self, first_name, last_name, email, phone, birthday, password, address=None):
if not address:
address = Address(street='', city='', state='')
address.save()
user = self.create_user(first_name=first_name, last_name=last_name, email=email, phone=phone, role='Worker', birthday=birthday, address=address, password=password)
user.is_admin = True
user.admin_permission = True
user.save()
return user
class User(AbstractBaseUser):
role_choices = (
('Worker', 'Worker'),
('Employer', 'Employer'),
)
objects = UserManager()
first_name = models.CharField(max_length=255, verbose_name='First Name')
last_name = models.CharField(max_length=255, verbose_name='Last Name')
email = models.EmailField(max_length=255, unique=True, verbose_name='Email Address')
phone = models.CharField(validators=[MinLengthValidator(10)], max_length=10, unique=True, verbose_name='Phone Number')
role = models.CharField(max_length=255, choices=role_choices, verbose_name='User Role')
birthday = models.DateField(auto_now=False, null=True, verbose_name='Birthday')
address = models.ForeignKey(Address, verbose_name='Address')
admin_permission = models.BooleanField()
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name', 'last_name', 'phone', 'birthday']
def get_full_name(self):
return self.first_name + ' ' + self.last_name
def get_short_name(self):
return self.first_name
@property
def is_superuser(self):
return self.is_admin
@property
def is_staff(self):
return self.is_admin
def has_perm(self, perm, obj=None):
return self.is_admin
def has_module_perms(self, app_label):
return self.is_admin
def __str__ (self):
return self.email
class Job(models.Model):
wage_type_choices = (
('Hourly', 'Hourly'),
('Total', 'Total'),
)
employer = models.ForeignKey(EmployerProfile, verbose_name='Employer')
address = models.ForeignKey(Address, verbose_name='Address')
title = models.CharField(max_length=255, verbose_name='Job Title')
description = models.TextField(blank=False, verbose_name='Job Description')
completion_date = models.DateField(auto_now=False, auto_now_add=False, verbose_name='Completion Date', help_text='mm/dd/yyyy')
wage_type = models.CharField(max_length=255, choices=wage_type_choices, verbose_name='Wage Type')
wage = models.DecimalField(validators=[MinValueValidator(9)], max_digits=6, decimal_places=2, verbose_name='Wage')
tools = models.TextField(blank=True, default='', verbose_name='Tools')
is_hired = models.BooleanField(default=False, verbose_name='Job Is Hired')
is_completed = models.BooleanField(default=False, verbose_name='Job Is Completed')
employer_is_reviewed = models.BooleanField(default=False, verbose_name='Employer Is Reviewed')
worker = models.ForeignKey(WorkerProfile, null=True, verbose_name='Worker')
def __str__ (self):
return self.title
我还包括了 Address 模型,因为这两个受影响的模型都有一个 ForeignKey to Address。
【问题讨论】:
Django 1.8 看起来不像您发布的屏幕截图。看起来您正在使用某种您没有提到的管理皮肤。 我正在使用 django_admin_bootstrapped 应用程序。我不认为它会产生影响,因为它是一个风格应用程序。感谢您指出这一点。 尝试在没有模型管理员的情况下注册模型,例如admin.site.register(User)
,看看是不是model admin有问题。如果这仍然不起作用,您应该显示不起作用的模型。
字段中似乎有些错误。 runserver 输出中有什么东西吗?尝试注释掉 search_fields 并分别排除每个问题模型。
This thread 在 Django 开发者邮件列表中可能是同样的问题。
【参考方案1】:
我将 Debug 设置为 True,但出现了 UnicodeError。问题是一些外国用户注册了,并且在他们的地址中使用了非英文字符。当用户模型访问这些地址时,管理员抛出了 UnicodeError。解决方案是从任何表中删除用户已输入外来字符的任何行,并验证用户将来仅在表单中输入允许的字符。
【讨论】:
以上是关于Django 管理员不会显示字段的主要内容,如果未能解决你的问题,请参考以下文章
在 django 中单击管理员的选择字段后如何隐藏或显示模型字段