django admin,显示日期

时间:2016-03-14 17:37:41

标签: python django django-admin

我一直在玩我的测试项目并遇到问题

(请忽略我的命名,因为我只是测试和初学者)

我有这个型号:

class APPLICANT_DATA(models.Model):

    class Meta:
        verbose_name = u'Applicant'
        verbose_name_plural = u'The Data Of Applicants'

    FIRST_NAME = models.CharField(max_length=20)
    LAST_NAME = models.CharField(max_length=20)
    MIDDLE_NAME = models.CharField(max_length=20)

    BIRTH_DAY = models.DateTimeField()

    ADDRESS = models.CharField(max_length=100)

    REGION = models.CharField(max_length=100)
    CITY = models.CharField(max_length=100)
    ZIP = models.CharField(max_length=100)

    EMAIL = models.CharField(max_length=100)
    PHONE_NUM = models.CharField(max_length=15)

    def DATE_OF_BIRTH(self):
        return self.BIRTH_DAY.strftime("%b %d %Y") 

admin.py:

class Applicant_List(APPLICANT_DATA):

    class  Meta:
        proxy = True
        verbose_name = u'Applicant'
        verbose_name_plural = u'Applicants'

class applicant(admin.ModelAdmin):
    model = Applicant_List
    list_display = ('FIRST_NAME', 'MIDDLE_NAME', 'LAST_NAME', 'EMAIL', 'DATE_OF_BIRTH', 'APPLICATION_DATE', 'APP_STATUS', 'SCHEDULED_AT', 'HAS_RECORD', 'JOB_CATEGORY', 'EMPLOYEE_ASSIGNED')

    def get_readonly_fields(self, request, obj=None):
        if obj:
            if not request.user.is_superuser:
                uname =  request.user.get_full_name()
                if str(obj.EMPLOYEE_ASSIGNED) != str(uname):

                    self.readonly_fields = [field.name for field in obj.__class__._meta.fields]
                    return self.readonly_fields
                else:
                    self.readonly_fields = ['FIRST_NAME', 'MIDDLE_NAME', 'LAST_NAME', 'EMAIL', 'JOB_CATEGORY', 'ADDRESS', 'REGION', 'CITY', 'ZIP', 'EMAIL', 'PHONE_NUM', 'DATE_OF_BIRTH']
                return self.readonly_fields
            else:
                self.readonly_fields = ['FIRST_NAME', 'MIDDLE_NAME', 'LAST_NAME', 'EMAIL', 'JOB_CATEGORY', 'ADDRESS', 'REGION', 'CITY', 'ZIP', 'EMAIL', 'PHONE_NUM', 'DATE_OF_BIRTH']
            return self.readonly_fields

我正在使用proxy我的模型,因为我需要将模型再次注册用于其他目的

在管理员工作正常,直到我使用get_readonly_fields

我正在使用get_readonly_fields仅在管理员用户不是超级用户时才能读取字段

错误说:

Unknown field(s) (DATE_OF_BIRTH) specified for Applicant_List. Check fields/fieldsets/exclude attributes of class applicant.

这是我的追溯:

Environment:


Request Method: GET
Request URL: http://localhost:8000/@dmin/transactions/applicant_list/22/

Django Version: 1.8.2
Python Version: 2.7.10
Installed Applications:
('grappelli',
 'django.contrib.admin',
 'frontend',
 'django.contrib.auth',
 'django.contrib.humanize',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.sessions.models',
 'file_maintenance',
 'reports',
 'transactions',
 'admin_reorder',
 'admin_notifications',
 'django_twilio')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.models.User',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware',
 'admin_reorder.middleware.ModelAdminReorder')


Traceback:

File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in wrapper
  616.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in _wrapped_view
  110.                     response = view_func(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func
  57.         response = view_func(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\contrib\admin\sites.py" in inner
 233.             return view(request, *args, **kwargs)
File "C:\Users\lolwat\Desktop\ITSWEBSITE\transactions\admin.py" in change_view
  211.         return super(applicant, self).change_view(request, object_id, form_url='', extra_context=extra_context)
File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in change_view
  1519.         return self.changeform_view(request, object_id, form_url, extra_context)
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in _wrapper
  34.             return bound_func(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in _wrapped_view
  110.                     response = view_func(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in bound_func
  30.                 return func.__get__(self, type(self))(*args2, **kwargs2)
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in inner
  145.                     return func(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in changeform_view
  1456.         ModelForm = self.get_form(request, obj)
File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in get_form
  703.                              % (e, self.__class__.__name__))

Exception Type: FieldError at /@dmin/transactions/applicant_list/22/
Exception Value: Unknown field(s) (DATE_OF_BIRTH) specified for Applicant_List. Check fields/fieldsets/exclude attributes of class applicant.

或者还有其他方法可以更改管理中的显示吗?

django 1.8

从我的管理界面删除grappelli和任何其他形式的添加上下文的新回溯(change_view,changelist_view等)

Environment:


Request Method: GET
Request URL: http://localhost:8000/@dmin/transactions/applicant_list/22/

Django Version: 1.8.2
Python Version: 2.7.10
Installed Applications:
('django.contrib.admin',
 'frontend',
 'django.contrib.auth',
 'django.contrib.humanize',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.sessions.models',
 'file_maintenance',
 'reports',
 'transactions',
 'admin_reorder',
 'admin_notifications',
 'django_twilio')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.models.User',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware',
 'admin_reorder.middleware.ModelAdminReorder')


Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in wrapper
  616.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in _wrapped_view
  110.                     response = view_func(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func
  57.         response = view_func(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\contrib\admin\sites.py" in inner
  233.             return view(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in change_view
  1519.         return self.changeform_view(request, object_id, form_url, extra_context)
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in _wrapper
  34.             return bound_func(*args, **kwargs)
 File "C:\Python27\lib\site-packages\django\utils\decorators.py" in _wrapped_view
  110.                     response = view_func(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in bound_func
  30.                 return func.__get__(self, type(self))(*args2, **kwargs2)
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in inner
  145.                     return func(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in changeform_view
  1456.         ModelForm = self.get_form(request, obj)
File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in get_form
  703.                              % (e, self.__class__.__name__))

Exception Type: FieldError at /@dmin/transactions/applicant_list/22/
Exception Value: Unknown field(s) (DATE_OF_BIRTH) specified for Applicant_List. Check fields/fieldsets/exclude attributes of class applicant.

0 个答案:

没有答案