使用 django non-rel 时未保存对象
我的 models.py :
class Projects(models.Model):
projectName =models.CharField(max_length = 100,unique=True,db_index=True)
projectManager = models.ForeignKey('Users')
class Users(models.Model):
name = models.CharField(max_length = 100,unique=True)
designation = models.CharField(max_length =100 )
team = ListField(models.CharField(max_length =100),null=True)
现在我正在尝试在 shell 中使用以下代码创建一个 Projects 类对象:
user_object = Users.objects.get(name="abc")
p = Projects(projectName="xyz",projectManager = user_object)
p.save()
在 p.save() 上它给了我错误:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/aprainfo/env/lib/python2.5/site-packages/Django-1.3.1-py2.5.egg/django/db/models/base.py", line 460, in save
self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/home/aprainfo/env/lib/python2.5/site-packages/Django-1.3.1-py2.5.egg/django/db/models/base.py", line 543, in save_base
for f in meta.local_fields if not isinstance(f, AutoField)]
File "/home/aprainfo/env/lib/python2.5/site-packages/Django-1.3.1-py2.5.egg/django/db/models/fields/subclassing.py", line 28, in inner
return func(*args, **kwargs)
File "/home/aprainfo/env/lib/python2.5/site-packages/Django-1.3.1-py2.5.egg/django/db/models/fields/related.py", line 876, in get_db_prep_save
connection=connection)
File "/home/aprainfo/env/lib/python2.5/site-packages/Django-1.3.1-py2.5.egg/django/db/models/fields/subclassing.py", line 28, in inner
return func(*args, **kwargs)
File "/home/aprainfo/env/lib/python2.5/site-packages/Django-1.3.1-py2.5.egg/django/db/models/fields/subclassing.py", line 28, in inner
return func(*args, **kwargs)
File "/home/aprainfo/env/lib/python2.5/site-packages/Django-1.3.1-py2.5.egg/django/db/models/fields/__init__.py", line 276, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "/home/aprainfo/env/lib/python2.5/site-packages/Django-1.3.1-py2.5.egg/django/db/models/fields/subclassing.py", line 53, in inner
return func(*args, **kwargs)
File "/home/aprainfo/env/lib/python2.5/site-packages/Django-1.3.1-py2.5.egg/django/db/models/fields/subclassing.py", line 53, in inner
return func(*args, **kwargs)
File "/home/aprainfo/env/lib/python2.5/site-packages/Django-1.3.1-py2.5.egg/django/db/models/fields/__init__.py", line 271, in get_db_prep_value
value = self.get_prep_value(value)
File "/home/aprainfo/env/lib/python2.5/site-packages/Django-1.3.1-py2.5.egg/django/db/models/fields/__init__.py", line 479, in get_prep_value
return int(value) ValueError: invalid literal for int() with base 10: '4f1116115fcff377b2000001'
我不知道为什么它试图在最后一行将 string 转换为 int 。我不知道如何摆脱这个错误?
My models.py :
class Projects(models.Model):
projectName =models.CharField(max_length = 100,unique=True,db_index=True)
projectManager = models.ForeignKey('Users')
class Users(models.Model):
name = models.CharField(max_length = 100,unique=True)
designation = models.CharField(max_length =100 )
team = ListField(models.CharField(max_length =100),null=True)
Now I am trying creating a Projects class object with this code in shell:
user_object = Users.objects.get(name="abc")
p = Projects(projectName="xyz",projectManager = user_object)
p.save()
On p.save() it gives me error:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/aprainfo/env/lib/python2.5/site-packages/Django-1.3.1-py2.5.egg/django/db/models/base.py", line 460, in save
self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/home/aprainfo/env/lib/python2.5/site-packages/Django-1.3.1-py2.5.egg/django/db/models/base.py", line 543, in save_base
for f in meta.local_fields if not isinstance(f, AutoField)]
File "/home/aprainfo/env/lib/python2.5/site-packages/Django-1.3.1-py2.5.egg/django/db/models/fields/subclassing.py", line 28, in inner
return func(*args, **kwargs)
File "/home/aprainfo/env/lib/python2.5/site-packages/Django-1.3.1-py2.5.egg/django/db/models/fields/related.py", line 876, in get_db_prep_save
connection=connection)
File "/home/aprainfo/env/lib/python2.5/site-packages/Django-1.3.1-py2.5.egg/django/db/models/fields/subclassing.py", line 28, in inner
return func(*args, **kwargs)
File "/home/aprainfo/env/lib/python2.5/site-packages/Django-1.3.1-py2.5.egg/django/db/models/fields/subclassing.py", line 28, in inner
return func(*args, **kwargs)
File "/home/aprainfo/env/lib/python2.5/site-packages/Django-1.3.1-py2.5.egg/django/db/models/fields/__init__.py", line 276, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "/home/aprainfo/env/lib/python2.5/site-packages/Django-1.3.1-py2.5.egg/django/db/models/fields/subclassing.py", line 53, in inner
return func(*args, **kwargs)
File "/home/aprainfo/env/lib/python2.5/site-packages/Django-1.3.1-py2.5.egg/django/db/models/fields/subclassing.py", line 53, in inner
return func(*args, **kwargs)
File "/home/aprainfo/env/lib/python2.5/site-packages/Django-1.3.1-py2.5.egg/django/db/models/fields/__init__.py", line 271, in get_db_prep_value
value = self.get_prep_value(value)
File "/home/aprainfo/env/lib/python2.5/site-packages/Django-1.3.1-py2.5.egg/django/db/models/fields/__init__.py", line 479, in get_prep_value
return int(value) ValueError: invalid literal for int() with base 10: '4f1116115fcff377b2000001'
I dont know why its trying to convert string into int in last line.I dont know how to get rid of this error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题很可能与 django non-rel 和 mongodb-engine 的安装有关。标准 django 期望所有 id 都是整数,而组成 django non-rel 的软件包套件则进行了更改,以允许 mongodb 使用基于字符串的 id。
我建议按照 http://django-mongodb.org/topics 上定义的顺序重新安装您的设置/setup.html
我也遇到了同样的问题,这是可能使用标准 django 而不是非相关版本的安装问题。
我的 pip requests.txt 包含以下条目:
然后我可以执行以下操作:
pip install -U -rrequirements.txt
Your problem is most likely with the installation of django non-rel and mongodb-engine. Standard django expects all ids to be integers whereas the suite of packages that make up django non-rel change that to allow the string-based id that mongodb uses.
I would suggest reinstalling your set up following the order defined on http://django-mongodb.org/topics/setup.html
I had this same issue and it was a matter of the installation possibly using the standard django instead of the non-rel one.
My pip requirements.txt has these entries:
And then I would be able to do:
pip install -U -r requirements.txt