我正在尝试将对象保存到数据库,但出现错误:

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'

最佳答案

问题是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的值。

最后,您知道整个脚本是一个肮脏的hack。从脚本操作Django的正确方法是编写custom Django admin commands。确实并不难,您将获得更强大,更可靠的解决方案。

08-04 22:29