出于测试目的,我创建了这样的2个模型。
class Customer(models.Model):
Created = models.DateField(help_text="user created time", auto_now_add=True)
GroupId = models.CharField("group Id", max_length=64, blank=True)
IsEnabled = models.BooleanField("is enabled")
IsStaff = models.BooleanField("is staff")
LastLogin = models.DateField(help_text="last login time", blank=True, null=True)
Login = models.EmailField(help_text="login email address", max_length=64)
Password = models.CharField(max_length=128)
PasswordFormat = models.CharField("password format", max_length=64, default="Django")
StayLoggedInFor = models.IntegerField("stay logged in for", blank=True, default=0)
#random_str is a function that generates random string
UserId = models.CharField("user Id", max_length=64, primary_key=True, editable=False, default=random_str)
class Profile(models.Model):
AutoPk = models.FloatField(primary_key=True, default=random.random, editable=False)
Customer = models.OneToOneField(Customer)
FirstName = models.CharField("first name", max_length=64, blank=True)
LastName = models.CharField("last name", max_length=64, blank=True)
这是我的admin.py
class ProfileInline(admin.StackedInline):
model = Profile
class UserAdmin(admin.ModelAdmin):
inlines = [ProfileInline]
admin.site.register(Customer, UserAdmin)
Django可以正确显示添加客户页面,但是当我点击“保存”时,它只是告诉我“请更正下面的错误”,并且没有任何进一步的信息。
然后我使用shell来添加客户及其配置文件而没有问题。
但是当我尝试在管理网站上编辑它们时,我在尝试保存时得到了这个调用堆栈。
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/admin/chancellor/customer/0.739365012869/
Django Version: 1.2.4
Python Version: 2.6.6
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.admin',
'chancellor']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "D:\Python26\lib\site-packages\django\core\handlers\base.py" in get_response
100. response = callback(request, *callback_args, **callback_kwargs)
File "D:\Python26\lib\site-packages\django\contrib\admin\options.py" in wrapper
265. return self.admin_site.admin_view(view)(*args, **kwargs)
File "D:\Python26\lib\site-packages\django\utils\decorators.py" in _wrapped_view
76. response = view_func(request, *args, **kwargs)
File "D:\Python26\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func
78. response = view_func(request, *args, **kwargs)
File "D:\Python26\lib\site-packages\django\contrib\admin\sites.py" in inner
190. return view(request, *args, **kwargs)
File "D:\Python26\lib\site-packages\django\utils\decorators.py" in _wrapper
21. return decorator(bound_func)(*args, **kwargs)
File "D:\Python26\lib\site-packages\django\utils\decorators.py" in _wrapped_view
76. response = view_func(request, *args, **kwargs)
File "D:\Python26\lib\site-packages\django\utils\decorators.py" in bound_func
17. return func(self, *args2, **kwargs2)
File "D:\Python26\lib\site-packages\django\db\transaction.py" in _commit_on_success
299. res = func(*args, **kw)
File "D:\Python26\lib\site-packages\django\contrib\admin\options.py" in change_view
916. queryset=inline.queryset(request))
File "D:\Python26\lib\site-packages\django\forms\models.py" in __init__
701. queryset=qs)
File "D:\Python26\lib\site-packages\django\forms\models.py" in __init__
427. super(BaseModelFormSet, self).__init__(**defaults)
File "D:\Python26\lib\site-packages\django\forms\formsets.py" in __init__
47. self._construct_forms()
File "D:\Python26\lib\site-packages\django\forms\formsets.py" in _construct_forms
97. self.forms.append(self._construct_form(i))
File "D:\Python26\lib\site-packages\django\forms\models.py" in _construct_form
714. form = super(BaseInlineFormSet, self)._construct_form(i, **kwargs)
File "D:\Python26\lib\site-packages\django\forms\models.py" in _construct_form
443. pk = self.data[pk_key]
File "D:\Python26\lib\site-packages\django\utils\datastructures.py" in __getitem__
235. raise MultiValueDictKeyError("Key %r not found in %r" % (key, self))
Exception Type: MultiValueDictKeyError at /admin/chancellor/customer/0.739365012869/
Exception Value: "Key 'profile-0-AutoPk' not found in <QueryDict: {u'profile-MAX_NUM_FORMS': [u'1'], u'StayLoggedInFor': [u'0'], u'profile-0-LastName': [u'Moss'], u'profile-0-Customer': [u'0.739365012869'], u'profile-__prefix__-FirstName': [u''], u'profile-TOTAL_FORMS': [u'1'], u'profile-__prefix__-LastName': [u''], u'GroupId': [u'1'], u'profile-__prefix__-Customer': [u'0.739365012869'], u'profile-0-FirstName': [u'Tom'], u'LastLogin': [u''], u'_save': [u'Save'], u'Login': [u'tom@c.com'], u'Password': [u'suishi'], u'PasswordFormat': [u'Django'], u'csrfmiddlewaretoken': [u'c510438295d20f91f25af3d6b061a918'], u'profile-INITIAL_FORMS': [u'1']}>"
我真的不需要使用管理站点,但是,我猜Django粉丝可能觉得这很有趣。