我试图将对象保存到数据库但我收到错误:
TypeError: int() argument must be a string or a number, not 'BuildsTable'
我有一个单独的脚本,应该使用许多主板名称和价格填充我的数据库。
我的models.py看起来像这样:
from django.db import models
# Create your models here.
class BuildsTable(models.Model):
id = models.AutoField(primary_key=True)
moboListing = models.CharField(max_length=200)
price = models.IntegerField()
我的数据库填充文件名为fillDB.py:
import sys
import os
if __name__ == "__main__":
sys.path.append('/home/waleedasif322/Documents/cb/computerbuilder_masterrepo/computerbuilder/')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "computerbuilder.settings")
from builds.models import BuildsTable
mobo = BuildsTable.objects.all()
print mobo
moboDB = open("db.txt", "r")
lines = moboDB.read().split('\",')
print lines
def main():
global lines
global BuildsTable
for item in lines:
try:
mobo = BuildsTable(moboListing="%s" % item[0])
except BuildsTable.DoesNotExist:
mobo = 1
try:
price_local = BuildsTable(moboListing="%s" % item[1])
except BuildsTable.DoesNotExist:
price_local = 1
"""
if(BuildsTable.objects.filter(
moboListing = mobo, price = price_local).exists() == False):
"""
mydb = BuildsTable(moboListing = mobo, price = price_local)
print mydb
mydb.save()
main()
如何使用主板列表填充数据库?
我的全部追溯:
Traceback (most recent call last):
File "fillDB.py", line 40, in <module>
main()
File "fillDB.py", line 38, in main
mydb.save()
File "/home/waleedasif322/Documents/cb/computerbuilder_masterrepo/cbenv/local/lib/python2.7/site-packages/django/db/models/base.py", line 545, in save
force_update=force_update, update_fields=update_fields)
File "/home/waleedasif322/Documents/cb/computerbuilder_masterrepo/cbenv/local/lib/python2.7/site-packages/django/db/models/base.py", line 573, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/home/waleedasif322/Documents/cb/computerbuilder_masterrepo/cbenv/local/lib/python2.7/site-packages/django/db/models/base.py", line 654, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/home/waleedasif322/Documents/cb/computerbuilder_masterrepo/cbenv/local/lib/python2.7/site-packages/django/db/models/base.py", line 687, in _do_insert
using=using, raw=raw)
File "/home/waleedasif322/Documents/cb/computerbuilder_masterrepo/cbenv/local/lib/python2.7/site-packages/django/db/models/manager.py", line 232, in _insert
return insert_query(self.model, objs, fields, **kwargs)
File "/home/waleedasif322/Documents/cb/computerbuilder_masterrepo/cbenv/local/lib/python2.7/site-packages/django/db/models/query.py", line 1511, in insert_query
return query.get_compiler(using=using).execute_sql(return_id)
File "/home/waleedasif322/Documents/cb/computerbuilder_masterrepo/cbenv/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 898, in execute_sql
for sql, params in self.as_sql():
File "/home/waleedasif322/Documents/cb/computerbuilder_masterrepo/cbenv/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 856, in as_sql
for obj in self.query.objs
File "/home/waleedasif322/Documents/cb/computerbuilder_masterrepo/cbenv/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 350, in get_db_prep
_save
prepared=False)
File "/home/waleedasif322/Documents/cb/computerbuilder_masterrepo/cbenv/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 342, in get_db_prep
_value
value = self.get_prep_value(value)
File "/home/waleedasif322/Documents/cb/computerbuilder_masterrepo/cbenv/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 1073, in get_prep_v
alue
return int(value)
TypeError: int() argument must be a string or a number, not 'BuildsTable'
答案 0 :(得分:1)
问题是无法保存mydb
,因为您正在将BuildsTable
对象设置为整数字段。考虑一下:
try:
price_local = BuildsTable(moboListing="%s" % item[1])
except BuildsTable.DoesNotExist:
price_local = 1
# ...
mydb = BuildsTable(moboListing = mobo, price = price_local)
您可以将price
设置为BuildsTable
而不是整数。这不会奏效。
顺便说一下你的其他try-catch也是可疑的,你设置mobo = BuildsTable(moboListing="%s" % item[0])
,然后使用mobo
作为moboListing
的值。
最后,你知道整个脚本是一个肮脏的黑客。从脚本中操作Django的正确方法是编写custom Django admin commands。它真的不难,你会得到更强大,更可靠的解决方案。