简体   繁体   English

Django TypeError:int()参数必须是字符串,类似字节的对象或数字,而不是'list'

[英]Django TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'

Newbie here. 新手在这里。 I'm writing a loader script for a website that uses a csv downloaded from wrapsnet.org. 我正在为使用从wrapsnet.org下载的csv的网站编写加载程序脚本。 When I try to run the loader I receive the message "TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'." 当我尝试运行加载程序时,我收到消息“TypeError:int()参数必须是字符串,类似字节的对象或数字,而不是'list'。”

Full traceback: 完全追溯:

  File "loader.py", line 30, in <module>
    ref.save()
  File "//anaconda/envs/django/lib/python3.6/site-packages/django/db/models/base.py", line 806, in save
force_update=force_update, update_fields=update_fields)
  File "//anaconda/envs/django/lib/python3.6/site-packages/django/db/models/base.py", line 836, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "//anaconda/envs/django/lib/python3.6/site-packages/django/db/models/base.py", line 922, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "//anaconda/envs/django/lib/python3.6/site-packages/django/db/models/base.py", line 961, in _do_insert
using=using, raw=raw)
  File "//anaconda/envs/django/lib/python3.6/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "//anaconda/envs/django/lib/python3.6/site-packages/django/db/models/query.py", line 1060, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
  File "//anaconda/envs/django/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1098, in execute_sql
for sql, params in self.as_sql():
  File "//anaconda/envs/django/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1051, in as_sql
for obj in self.query.objs
  File "//anaconda/envs/django/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1051, in <listcomp>
for obj in self.query.objs
  File "//anaconda/envs/django/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1050, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
  File "//anaconda/envs/django/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 990, in prepare_value
value = field.get_db_prep_save(value, connection=self.connection)
  File "//anaconda/envs/django/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 766, in get_db_prep_save
prepared=False)
  File "//anaconda/envs/django/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 758, in get_db_prep_value
value = self.get_prep_value(value)
  File "//anaconda/envs/django/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 1849, in get_prep_value
return int(value)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'

Loader.py: Loader.py:

import os, sys, string, csv, datetime, time, django, pytz

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "refugees.settings")

django.setup()

from reports.models import Country, State, City, RefugeeReport

from django.template.defaultfilters import slugify, urlize
from django.core.exceptions import ObjectDoesNotExist

reader = csv.reader(open("refugees0216.csv"), dialect=csv.excel)
next(reader)
for row in reader:

    refstate = row[0]
    refstate_slug = slugify(refstate)
    refstateid, refstateadded = State.objects.get_or_create(name=refstate, name_slug=refstate_slug)

    reforigin = row[3]
    reforigin_slug = slugify(reforigin)
    reforiginid, reforiginadded = Country.objects.get_or_create(name=reforigin, name_slug=reforigin_slug)

    popcity = row[6]
    popcity_slug = slugify (popcity)
    popcityid, popcityadded = City.objects.get_or_create(name=popcity, name_slug=popcity_slug)

    ref = RefugeeReport(country=reforiginid, state=refstateid, city=popcityid, year=row[1], city_total=row[6], state_total=row[2], country_total=row[4], all_countries=[7])
    print(ref)
    ref.save()

Models.py: Models.py:

from django.db import models

class Country(models.Model):
    name=models.CharField(max_length=255)
    name_slug=models.SlugField()
    def __str__(self):
        return self.name

class State(models.Model):
    name=models.CharField(max_length=255)
    name_slug=models.SlugField()
    def __str__(self):
    return self.name
    def get_absolute_url(self):
        return "/reports/%s" % self.name_slug

class City(models.Model):
    state=models.ForeignKey(State)
    name=models.CharField(max_length=255)
    name_slug=models.SlugField()
    def __str__(self):
        return self.name
    def get_absolute_url(self):
        return "/reports/%s/%s" % (self.state.name_slug, self.name_slug)

class RefugeeReport(models.Model):
    country=models.ForeignKey(Country)
    state=models.ForeignKey(State)
    city=models.ForeignKey(City)
    year=models.CharField(max_length=255)
    city_total=models.IntegerField(default=0)
    state_total=models.IntegerField(default=0)
    country_total=models.IntegerField(default=0)
    all_countries=models.IntegerField(default=0)
    def __str__(self):
        return "%s, %s from %s" % (self.city.name_slug, self.state.name_slug, self.year)
    def get_absolute_url(self):
        return "/reports/%s/%s/%s" % (self.city.name_slug, self.state.name_slug, self.year)

I would appreciate any suggestions you have to make it work. 我很感激你有任何建议让它成功。

Update: traceback for printing all variables 更新:用于打印所有变量的回溯

  File "loader.py", line 29, in <module>
    print(ref, country, state, city, year, city_total, state_total, country_total, all_countries)
NameError: name 'country' is not defined

I think you have error on this line: 我认为你在这一行上有错误:

  ref = RefugeeReport(country=reforiginid, state=refstateid, city=popcityid, year=row[1], city_total=row[6], state_total=row[2], country_total=row[4], all_countries=[7])

Take a look at all_countries property, I think that it's incorrect: 看看all_countries属性,我认为这是不正确的:

all_countries=[7]

Change it to: 将其更改为:

 all_countries=row[7]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 int()参数必须是字符串,类似字节的对象或数字,而不是“列表” - int() argument must be a string, a bytes-like object or a number, not 'list' Django: TypeError: int() argument must be a string, a bytes-like object or a number, not 'Driver' - Django : TypeError: int() argument must be a string, a bytes-like object or a number, not 'Driver' Django - /statements/ int() 参数处的 TypeError 必须是字符串、类似字节的对象或数字,而不是“AnonymousUser” - Django - TypeError at /statements/ int() argument must be a string, a bytes-like object or a number, not 'AnonymousUser' Django REST 错误:TypeError: int() 参数必须是字符串、类似字节的对象或数字,而不是“BoundField” - Django REST error: TypeError: int() argument must be a string, a bytes-like object or a number, not 'BoundField' Django: TypeError: int() argument must be a string, a bytes-like object or a number, not 'ObjectId' - Django: TypeError: int() argument must be a string, a bytes-like object or a number, not 'ObjectId' Django保存到数据库:TypeError:int()参数必须是字符串,类似字节的对象或数字,而不是“元组” - Django save to DB: TypeError: int() argument must be a string, a bytes-like object or a number, not 'tuple' Django: TypeError: int() 参数必须是字符串、类似字节的对象或数字,而不是 - Django: TypeError: int() argument must be a string, a bytes-like object or a number, not 类型错误:int() 参数必须是字符串、类似字节的对象或数字,而不是“列表” - TypeError: int() argument must be a string, a bytes-like object or a number, not 'list' Python 2-TypeError:int()参数必须是字符串,类似字节的对象或数字,而不是“列表” - Python 2 - TypeError: int() argument must be a string, a bytes-like object or a number, not 'list' TypeError at /students/exam/1/ int() 参数必须是字符串、类似字节的对象或数字,而不是“列表” - TypeError at /students/exam/1/ int() argument must be a string, a bytes-like object or a number, not 'list'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM