Django Admin不会显示字段

时间:2016-03-06 17:09:24

标签: python mysql django django-admin database-migration

在我的实时网站上的Django Admin中,编辑某些模型的表单是空白的。黄色栏下应该有字段:

Example

出于某种原因,这仅影响两个模型(用户和作业),其他模型工作正常。在本地测试服务器上测试网站时,也会显示受影响模型上的字段。这是我的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。

1 个答案:

答案 0 :(得分:1)

我将Debug设置为True,并且存在UnicodeError。问题是一些外国用户注册了,并在他们的地址中使用了非英文字符。当用户模型访问这些地址时,管理员抛出了UnicodeError。解决方案是从用户输入外来字符的任何表中删除任何行,并验证用户以后只在表单中输入允许的字符。