2

尝试查看管理页面时出现此错误:

InvalidId at /admin/

AutoField (default primary key) values must be strings representing an ObjectId on MongoDB (got u'1' instead). Please make sure your SITE_ID contains a valid ObjectId string.

Request Method:     GET
Request URL:    http://127.0.0.1:8000/admin/
Django Version:     1.3
Exception Type:     InvalidId
Exception Value:    

AutoField (default primary key) values must be strings representing an ObjectId on MongoDB (got u'1' instead). Please make sure your SITE_ID contains a valid ObjectId string.
...

所以我四处搜索并认为我需要执行以下操作

 ./manage.py tellsiteid
The default site's ID is u'50c5766e62f11f41f0332e65'. To use the sites framework, add this line to settings.py:
SITE_ID=u'50c5766e62f11f41f0332e65'

所以我将它添加到我的settings.py

DEBUG = True
TEMPLATE_DEBUG = DEBUG
SITE_ID = u'50c5766e62f11f41f0332e65'
...

我仍然收到相同的错误消息。我应该运行syncdb或重新启动服务器吗?因为我两者都做了,但仍然遇到同样的错误。

编辑

我试过SITE_ID在 Python shell 中查看 的值,果然……

$ python manage.py shell
Python 2.7.3 (default, Aug  1 2012, 05:14:39) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from concepts.settings import *
>>> SITE_ID
1

git diff清楚地表明我已经更改了设置文件,对吧?

$ git diff
diff --git a/settings.py b/settings.py
index 586ea3a..bb069ed 100644
--- a/settings.py
+++ b/settings.py
@@ -2,6 +2,7 @@

 DEBUG = True
 TEMPLATE_DEBUG = DEBUG
+SITE_ID = u'50c5766e62f11f41f0332e65'

 ADMINS = (
     # ('Your Name', 'your_email@example.com'),
 ...

我在这里做错了什么?

4

2 回答 2

5

哦,我傻了。这些代码行后来出现在settings.py

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'

SITE_ID = 1

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True
...

所以我只是将那里的 1 更改为u'50c5766e62f11f41f0332e65'现在它可以工作了!

于 2012-12-10T08:05:18.080 回答
0

我遇到了同样的问题,但即使我在settings.py中更改了 SITE_ID,问题仍然存在。我在 INSTALLED_APPS 中评论了 'django.contrib.sites' 行,然后 syncdb 正确完成(在我看来,站点应用程序是一个几乎没有人需要的应用程序......如此快速和干净的解决方案:))

于 2013-11-15T13:09:27.427 回答