我为django项目构建了一个AbstractUser模型的扩展,我有几个应该自动填充的字段,一个唯一的字符串,一个创建和修改的时间戳。
我使用包django-model-utils中的字段来创建和修改工作,并且我已根据相同的原则为代码构建了自己的自定义字段。
django管理员似乎无法找到这些字段。
除了django-admin之外,这些字段似乎无处不在。
以下是代码的准系统:
models.py 来自django.contrib.auth.models导入AbstractUser
from model_utils import Choices
from model_utils.fields import AutoCreatedField, AutoLastModifiedField
from .fields import CoombaCodeField
class CoombaUser(AbstractUser):
# class CoombaUser(models.Model):
# Timestamp. Created timestamp is suppled by AbstractUser, as date_joined
modified = AutoLastModifiedField(_('modified'))
# string identifer, used in referrals
coomba_code = CoombaCodeField()
fields.py
from django.db import models, OperationalError
from core.keyGenerators import ShortKeyGenerator, KeyGenerator
from .settings import COOMBA_CODE_COUNTER_KEY, COOMBA_CODE_COUNTER_MINIMUM
# Impossible to call code accessing databases before databases are created.
# Code below moves database call to first use.
coomba_code_generator = None
def get_key():
global coomba_code_generator
if(coomba_code_generator is None):
coomba_code_generator = ShortKeyGenerator(
charset=KeyGenerator.CHARSET_FULL,
counter_key=COOMBA_CODE_COUNTER_KEY,
smallest_allowed_value=COOMBA_CODE_COUNTER_MINIMUM
)
key = coomba_code_generator.get_key()
return key
class CoombaCodeField(models.CharField):
def __init__(self, *args, **kwargs):
kwargs.setdefault('editable', False)
kwargs.setdefault('default', get_key)
kwargs.setdefault('max_length', 30)
super(CoombaCodeField, self).__init__(*args, **kwargs)
admin.py 来自django.contrib import admin
from .models import CoombaUser
def full_name(user):
return (user.first_name + " " + user.last_name)
class CoombaUserAdmin(admin.ModelAdmin):
list_display = ("email", full_name, "date_joined")
fieldsets = (
("CoombaUser", {
'fields': (
"email",
("first_name", "last_name"),
"nickname",
"coomba_code",
"social_security_number",
"gender",
"date_of_birth",
"account_status",
"last_login",
),
}),
)
admin.site.register(CoombaUser, CoombaUserAdmin)
错误:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/coombaUsers/coombauser/1/
Django Version: 1.6.5
Python Version: 2.7.6
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'core',
'coombaUsers',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.google',
'allauth.socialaccount.providers.linkedin',
'djangular',
'localflavor',
'parsley',
'crispy_forms',
'south',
'django_coverage')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "C:\Users\Sverker\.virtualenvs\coomba\lib\site-packages\django\core\handlers\base.py" in get_response
112. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Sverker\.virtualenvs\coomba\lib\site-packages\django\contrib\admin\options.py" in wrapper
432. return self.admin_site.admin_view(view)(*args, **kwargs)
File "C:\Users\Sverker\.virtualenvs\coomba\lib\site-packages\django\utils\decorators.py" in _wrapped_view
99. response = view_func(request, *args, **kwargs)
File "C:\Users\Sverker\.virtualenvs\coomba\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func
52. response = view_func(request, *args, **kwargs)
File "C:\Users\Sverker\.virtualenvs\coomba\lib\site-packages\django\contrib\admin\sites.py" in inner
198. return view(request, *args, **kwargs)
File "C:\Users\Sverker\.virtualenvs\coomba\lib\site-packages\django\utils\decorators.py" in _wrapper
29. return bound_func(*args, **kwargs)
File "C:\Users\Sverker\.virtualenvs\coomba\lib\site-packages\django\utils\decorators.py" in _wrapped_view
99. response = view_func(request, *args, **kwargs)
File "C:\Users\Sverker\.virtualenvs\coomba\lib\site-packages\django\utils\decorators.py" in bound_func
25. return func(self, *args2, **kwargs2)
File "C:\Users\Sverker\.virtualenvs\coomba\lib\site-packages\django\db\transaction.py" in inner
371. return func(*args, **kwargs)
File "C:\Users\Sverker\.virtualenvs\coomba\lib\site-packages\django\contrib\admin\options.py" in change_view
1206. ModelForm = self.get_form(request, obj)
File "C:\Users\Sverker\.virtualenvs\coomba\lib\site-packages\django\contrib\admin\options.py" in get_form
531. % (e, self.__class__.__name__))
Exception Type: FieldError at /admin/coombaUsers/coombauser/1/
Exception Value: Unknown field(s) (coomba_code) specified for CoombaUser. Check fields/fieldsets/exclude attributes of class CoombaUserAdmin.
答案 0 :(得分:1)
不确定这是否会解决您的错误,但为什么要定义:
def full_name(user):
...
以外:
class CoombaUserAdmin(admin.ModelAdmin):
....
如何将用户对象传递给full_name()
我会这样做:
class CoombaUserAdmin(admin.ModelAdmin):
list_display = ("email", "full_name", "date_joined")
def full_name(self, obj):
return u'%s %s' % (obj.first_name, obj.last_name)
full_name.short_description = "Full Name"
doc:
中的示例class PersonAdmin(admin.ModelAdmin):
list_display = ('upper_case_name',)
def upper_case_name(self, obj):
return ("%s %s" % (obj.first_name, obj.last_name)).upper()
upper_case_name.short_description = 'Name'