对于带有基数为10的int(),文字无效:''创建对象时

时间:2016-07-07 18:43:55

标签: python django model

我试图从CSV文件中读取并在Django中创建一个对象(1.9,Py 3.5)但是我收到此错误,无论我将字段更改为

  

基数为10的int()的文字无效:''

这一行是:

  

其他=行['其他']

site = Site.objects.create(consolidated_financials      = row['Consolidated financials'],
                                  type                          = Type.objects.get_or_create(name=row['Type'])[0],
                                  tier1_business                = Business.objects.get_or_create(tier=1, name=row['Tier-1 business'])[0],
                                  tier2_business                = Business.objects.get_or_create(tier=2, name=row['Tier-2 business'])[0],
                                  tier3_business                = Business.objects.get_or_create(tier=2, name=row['Tier-3 business'])[0],
                                  site_name                     = row['Site Name'],
                                  site_id                       = row['Site ID'],
                                  region                        = Region.objects.get_or_create(name=row['Region'])[0],
                                  country                       = Country.objects.get_or_create(name=row['Country'], region=Region.objects.get_or_create(name=row['Region'])[0])[0],
                                  city                          = City.objects.get_or_create(name=row['City'], country=Country.objects.get_or_create(name=row['Country'], region=Region.objects.get_or_create(name=row['Region'])[0])[0])[0],
                                  site_type                     = SiteType.objects.get_or_create(name=row['Type of site?'])[0],
                                  remote_site                   = row['Remote site?'],
                                  finance_manager_name          = row['Finance Manager Name'],
                                  finance_manager_sso           = row['Finance Manager SSO'],
                                  quarter                       = row['Quarter'],
                                  revenue                       = row['Revenue'],
                                  supply_chain_manager_name     = row['Supply Chain Manager Name'],
                                  supply_chain_manager_sso      = row['Supply Chain Manager SSO'], 
                                  product_lines                 = row['Product Lines'],
                                  manufacturing_processes       = row['Manufacturing Processes'],
                                  factory_utilization           = row['Factory Utilization'],
                                  fte                           = row['FTE'],
                                  hourly                        = row['Hourly'],
                                  salaried                      = row['Salaried'],
                                  other                         = row['Other']
                                  ) 

网站模型:

class Site(models.Model):
"""
Model for a site entry
@author: Leonardo Pessoa
@since: 05/09/2016 
"""
from decimal import Decimal

consolidated_financials     = models.BooleanField()
type                        = models.ForeignKey(Type)
tier1_business              = models.ForeignKey(Business, limit_choices_to = {'tier': 1}, related_name='%(class)s_tier1')
tier2_business              = models.ForeignKey(Business, limit_choices_to = {'tier': 2}, related_name='%(class)s_tier2')
tier3_business              = models.ForeignKey(Business, limit_choices_to = {'tier': 3}, related_name='%(class)s_tier3')
site_name                   = models.CharField(max_length = 150, unique=True)
site_id                     = models.IntegerField()
region                      = models.ForeignKey(Region)
country                     = models.ForeignKey(Country)
city                        = models.ForeignKey(City)
site_type                   = models.ForeignKey(SiteType)
remote_site                 = models.BooleanField()
finance_manager_name        = models.CharField(max_length = 50)
finance_manager_sso         = models.IntegerField()
quarter                     = models.DecimalField(max_digits = 12, decimal_places = 2, default=Decimal('0.0'))
revenue                     = models.DecimalField(max_digits = 12, decimal_places = 2, default=Decimal('0.0'))
supply_chain_manager_name   = models.CharField(max_length = 50, default='')
supply_chain_manager_sso    = models.IntegerField(default=000000000)
product_lines               = models.CharField(max_length = 100, default='')
manufacturing_processes     = models.TextField(max_length = 500, default='')
factory_utilization         = models.DecimalField(max_digits = 5, decimal_places = 2, default=Decimal('0.0'))
fte                         = models.IntegerField()
hourly                      = models.IntegerField()
salaried                    = models.IntegerField()
other                       = models.TextField(max_length = 500, default='')
ges_id                      = models.CharField(max_length = 20)
latitude                    = models.DecimalField(max_digits = 10, decimal_places=7, default=Decimal('0.0'))
longitude                   = models.DecimalField(max_digits = 10, decimal_places=7, default=Decimal('0.0'))

行:

row 
{'City': 'xxxxxxx',
 'Consolidated financials': 'True',
 'Country': 'Argentina (AR)',
 'FTE': '',
 'Factory Utilization': '',
 'Finance Manager Name': '',
 'Finance Manager SSO': '',
 'Hourly': '',
 'Manufacturing Processes': '',
 'Other': '',
 'Product Lines': '',
 'Quarter': '',
 'Region': 'Latin America',
 'Remote site?': 'True',
 'Revenue': '',
 'Salaried': '',
 'Site ID': '12312',
 'Site Name': 'xxxxxxxxx',
 'Supply Chain Manager Name': '',
 'Supply Chain Manager SSO': '',
 'Tier-1 business': 'xxxxxxxxxx',
 'Tier-2 business': 'xxxxxxxxxxxxx',
 'Tier-3 business': 'Latin America',
 'Type': 'xxxxxx xxxxx',
 'Type of site?': 'Other'}

我知道代码有很大的性能优化空间,但我只是想先证明一下这个功能。

谢谢!

1 个答案:

答案 0 :(得分:1)

问题是,您的Site模型希望other成为int(模型是other = IntegerField还是类似?),并且您提供的是空字符串。最简单的解决方法是将row['Other']更改为row['Other'] or 0

如果您知道您将获得非数字值作为一般规则,那么您可以添加基本逻辑来测试非数字,或者将IntegerField更新为可以接受文本的内容。 A list of valid Django fields can be found here

# An example of conditional logic to test for a non-number and use 0 if so
other = row['Other'] if row['Other'] and row['Other'].isdigit() else 0

修改

查看您的模型,问题可能不在于其他字段,但仍存在打字问题。例如Supply Chain Manager SSO应该是一个int,但你肯定传递了''