Django管理网站没有';不能使用OneToonefied?

Django管理网站没有';不能使用OneToonefied?,django,Django,出于测试目的,我创建了两个这样的模型 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")

出于测试目的,我创建了两个这样的模型

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)
这是我的管理员

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']}>"
环境:
申请方式:邮寄
请求URL:http://127.0.0.1:8000/admin/chancellor/customer/0.739365012869/
Django版本:1.2.4
Python版本:2.6.6
已安装的应用程序:
['django.contrib.auth',
“django.contrib.contenttypes”,
“django.contrib.sessions”,
“django.contrib.sites”,
“django.contrib.messages”,
“django.contrib.admin”,
“议长”]
已安装的中间件:
('django.middleware.common.CommonMiddleware',
“django.contrib.sessions.middleware.SessionMiddleware”,
“django.middleware.csrf.CsrfViewMiddleware”,
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
回溯:
get\U响应中的文件“D:\Python26\lib\site packages\django\core\handlers\base.py”
100响应=回调(请求,*回调参数,**回调参数)
包装器中的文件“D:\Python26\lib\site packages\django\contrib\admin\options.py”
265返回self.admin\u site.admin\u视图(视图)(*args,**kwargs)
视图中的文件“D:\Python26\lib\site packages\django\utils\decorators.py”
76响应=查看功能(请求,*args,**kwargs)
文件“D:\Python26\lib\site packages\django\views\decorators\cache.py”位于\u wrapped\u view\u func中
78响应=查看功能(请求,*args,**kwargs)
文件“D:\Python26\lib\site packages\django\contrib\admin\sites.py”位于内部
190返回视图(请求、*args、**kwargs)
_包装中的文件“D:\Python26\lib\site packages\django\utils\decorators.py”
21返回装饰符(绑定函数)(*args,**kwargs)
视图中的文件“D:\Python26\lib\site packages\django\utils\decorators.py”
76响应=查看功能(请求,*args,**kwargs)
绑定函数中的文件“D:\Python26\lib\site packages\django\utils\decorators.py”
17返回函数(self,*args2,**kwargs2)
文件“D:\Python26\lib\site packages\django\db\transaction.py”位于\u commit\u on \u success
299res=func(*参数,**kw)
更改视图中的文件“D:\Python26\lib\site packages\django\contrib\admin\options.py”
916queryset=inline.queryset(请求))
\uuu init中的文件“D:\Python26\lib\site packages\django\forms\models.py”__
701queryset=qs)
\uuu init中的文件“D:\Python26\lib\site packages\django\forms\models.py”__
427super(BaseModelFormSet,self)。\uuuuu init\uuuuu(**默认值)
_uuinit中的文件“D:\Python26\lib\site packages\django\forms\formsets.py”__
47自我建构形式()
文件“D:\Python26\lib\site packages\django\forms\formsets.py”位于\u construct\u forms中
97self.forms.append(self.\u-construct\u-form(i))
文件“D:\Python26\lib\site packages\django\forms\models.py”的格式为
714形式=超级(BaseInlineFormSet,self)。\u构造\u形式(i,**kwargs)
文件“D:\Python26\lib\site packages\django\forms\models.py”的格式为
443pk=自身数据[pk\U键]
_uugetItem中的文件“D:\Python26\lib\site packages\django\utils\datastructures.py”__
235引发多值DictKeyError(“在%r中找不到键%r”%(键,自身))
异常类型:多值DictKeyError位于/admin/Celler/customer/0.739365012869/
异常值:“在中找不到键“profile-0-AutoPk”

我真的不需要使用管理站点,但是,我想Django的粉丝可能会觉得这很有趣。

一个随机的浮点值作为主键?WTF?你认为这是问题所在吗。我试图将一些JSON解析为Django的ORM对象。在这个过程中,我发现要建立对象之间的关系,其中一些必须有主键。