Peewee - 以 10 为底的 int() 的无效文字:'' 在循环查询时
Peewee - invalid literal for int() with base 10: '' when looping over query
我正在尝试遍历 Peewee 查询对象,当我尝试访问循环查询的模板时出现以下错误:
ValueError: invalid literal for int() with base 10: ''
您将在下面看到视图函数和模板的完整回溯和代码。我什至尝试将模板循环简化为:
{% for company in companies %}
<p>{{ company.company_name }}</p>
{% endfor %}
我仍然遇到同样的错误。
完整追溯
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/Library/Python/2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/Library/Python/2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/Library/Python/2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Library/Python/2.7/site-packages/flask_login.py", line 758, in decorated_view
return func(*args, **kwargs)
File "/Users/wyssuser/Desktop/all_my_projects/new_danish/app/views.py", line 143, in index
return render_template('new_batch.html', companies=companies)
File "/Library/Python/2.7/site-packages/flask/templating.py", line 128, in render_template
context, ctx.app)
File "/Library/Python/2.7/site-packages/flask/templating.py", line 110, in _render
rv = template.render(context)
File "/Library/Python/2.7/site-packages/jinja2/environment.py", line 969, in render
return self.environment.handle_exception(exc_info, True)
File "/Library/Python/2.7/site-packages/jinja2/environment.py", line 742, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/wyssuser/Desktop/all_my_projects/new_danish/app/templates/new_batch.html", line 2, in top-level template code
{% extends "base.html" %}
File "/Users/wyssuser/Desktop/all_my_projects/new_danish/app/templates/base.html", line 1, in top-level template code
{% extends "bootstrap/base.html" %}
File "/Library/Python/2.7/site-packages/flask_bootstrap/templates/bootstrap/base.html", line 1, in top-level template code
{% block doc -%}
File "/Library/Python/2.7/site-packages/flask_bootstrap/templates/bootstrap/base.html", line 4, in block "doc"
{%- block html %}
File "/Library/Python/2.7/site-packages/flask_bootstrap/templates/bootstrap/base.html", line 20, in block "html"
{% block body -%}
File "/Users/wyssuser/Desktop/all_my_projects/new_danish/app/templates/base.html", line 95, in block "body"
{% block content %}{% endblock %}
File "/Users/wyssuser/Desktop/all_my_projects/new_danish/app/templates/new_batch.html", line 30, in block "content"
{% for company in companies %}
File "/Library/Python/2.7/site-packages/peewee.py", line 1772, in next
obj = self.iterate()
File "/Library/Python/2.7/site-packages/peewee.py", line 1760, in iterate
return self.process_row(row)
File "/Library/Python/2.7/site-packages/peewee.py", line 1833, in process_row
setattr(instance, column, func(row[i]))
File "/Library/Python/2.7/site-packages/peewee.py", line 735, in python_value
return value if value is None else self.coerce(value)
ValueError: invalid literal for int() with base 10: ''
查看代码
@app.route('/', methods=("POST", 'GET'))
@login_required
def index():
form = GetClientsForm()
if form.validate_on_submit():
if form.sectors.data == 'Realtors':
companies = Realtor.select().where((Realtor.have_contacted != True) & (Realtor.dont_contact_anymore != True))
return render_template('new_batch.html', companies=companies)
else: #Biotech/Engineering
companies = Company.select().where((Company.have_contacted != True) & (Company.dont_contact_anymore != True))
return render_template('new_batch.html', companies=companies)
模板代码
...
{% for company in companies %}
<tr class="company-row {{ company.id }}" data-toggle="collapse" data-target="#{{ company.id }}-info" id="row-{{ company.id }}" data-company-id="{{ company.id }}">
<td class="col-md-3 company-name">
<input type="text" value="{{ company.company_name }}" id="{{ company.id }}-company-name" class="form-control" name="company-name">
</td>
<td class="col-md-2 company-website">
<div>
<a href="{{ company.website }}" target="blank_">{{ company.website }}</a>
</div>
</td>
<td class="col-md-3 company-email">
<input type="text" value="{{ company.email_address }}" id="{{ company.id }}-company-email" class="form-control" name="company-email">
</td>
<td class="col-md-1 company-contact-form">
<div class="checkbox">
<input type="checkbox" id="{{ company.id }}-contact-form" name="contact-form" data-url='contact_form_company'>
</div>
</td>
<td class="col-md-1 contact-company">
{{ render_submit('E-mail', class="contact-button", data='data-url=contact_company') }}
</td>
<td class="col-md-1 remove-company">
{{ render_submit("Don't Contact", class="dont-contact-button", data='data-url=dont_contact_company') }}
</td>
<td class="col-md-1 company-page">
<div>
<a href="{{ url_for('company_page', company_id=company.id) }}">{{ render_submit('Company Page') }}</a>
</div>
</td>
</tr>
{% endfor %}
...
更新 - 型号代码
class Company(Model):
dont_contact_anymore = BooleanField(default=False)
company_name = CharField()
website = CharField(unique=True)
email_address = CharField()
country = CharField()
scraped_on = DateTimeField(formats="%m-%d-%Y")
have_contacted = BooleanField(default=False)
current_pipeline_phase = IntegerField(default=0)
day_0_message_id = IntegerField()
day_0_contacted_by = ForeignKeyField(
rel_model=User,
related_name='contacted_by_day_0',
db_column='day_0_contacted_by'
)
day_0_emails_contacted = CharField()
day_0_response = IntegerField()
day_0_sent = DateTimeField(formats="%m-%d-%Y")
day_5_message_id = IntegerField()
day_5_contacted_by = ForeignKeyField(
rel_model=User,
related_name='contacted_by_day_5',
db_column='day_5_contacted_by'
)
day_5_emails_contacted = CharField()
day_5_response = IntegerField()
day_5_sent = DateTimeField(formats="%m-%d-%Y")
day_35_message_id = IntegerField()
day_35_contacted_by = ForeignKeyField(
rel_model=User,
related_name='contacted_by_day_35',
db_column='day_35_contacted_by'
)
day_35_emails_contacted = CharField()
day_35_response = IntegerField()
day_35_sent = DateTimeField(formats="%m-%d-%Y")
day_125_message_id = IntegerField()
day_125_contacted_by = ForeignKeyField(
rel_model=User,
related_name='contacted_by_day_125',
db_column='day_125_contacted_by'
)
day_125_emails_contacted = CharField()
day_125_response = IntegerField()
day_125_sent = DateTimeField(formats="%m-%d-%Y")
batch = IntegerField()
sector = CharField()
class Meta:
database = DATABASE
order_by = ('have_contacted',)
@classmethod
def create_company(cls, company_name, website, email_address):
try:
with DATABASE.transaction():
cls.create(company_name=company_name, website=website, email_address=email_address, scraped_on=datetime.now)
print 'Saved {}'.format(company_name)
except IntegrityError:
print '{} already exists in the database'.format(company_name)
您的模型似乎声明了 IntegerField
,但数据库中存储了一个空字符串。也许你可以去清理你的数据库:
UPDATE whatever SET int_field = NULL WHERE int_field = '';
我正在尝试遍历 Peewee 查询对象,当我尝试访问循环查询的模板时出现以下错误:
ValueError: invalid literal for int() with base 10: ''
您将在下面看到视图函数和模板的完整回溯和代码。我什至尝试将模板循环简化为:
{% for company in companies %}
<p>{{ company.company_name }}</p>
{% endfor %}
我仍然遇到同样的错误。
完整追溯
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/Library/Python/2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/Library/Python/2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/Library/Python/2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Library/Python/2.7/site-packages/flask_login.py", line 758, in decorated_view
return func(*args, **kwargs)
File "/Users/wyssuser/Desktop/all_my_projects/new_danish/app/views.py", line 143, in index
return render_template('new_batch.html', companies=companies)
File "/Library/Python/2.7/site-packages/flask/templating.py", line 128, in render_template
context, ctx.app)
File "/Library/Python/2.7/site-packages/flask/templating.py", line 110, in _render
rv = template.render(context)
File "/Library/Python/2.7/site-packages/jinja2/environment.py", line 969, in render
return self.environment.handle_exception(exc_info, True)
File "/Library/Python/2.7/site-packages/jinja2/environment.py", line 742, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/wyssuser/Desktop/all_my_projects/new_danish/app/templates/new_batch.html", line 2, in top-level template code
{% extends "base.html" %}
File "/Users/wyssuser/Desktop/all_my_projects/new_danish/app/templates/base.html", line 1, in top-level template code
{% extends "bootstrap/base.html" %}
File "/Library/Python/2.7/site-packages/flask_bootstrap/templates/bootstrap/base.html", line 1, in top-level template code
{% block doc -%}
File "/Library/Python/2.7/site-packages/flask_bootstrap/templates/bootstrap/base.html", line 4, in block "doc"
{%- block html %}
File "/Library/Python/2.7/site-packages/flask_bootstrap/templates/bootstrap/base.html", line 20, in block "html"
{% block body -%}
File "/Users/wyssuser/Desktop/all_my_projects/new_danish/app/templates/base.html", line 95, in block "body"
{% block content %}{% endblock %}
File "/Users/wyssuser/Desktop/all_my_projects/new_danish/app/templates/new_batch.html", line 30, in block "content"
{% for company in companies %}
File "/Library/Python/2.7/site-packages/peewee.py", line 1772, in next
obj = self.iterate()
File "/Library/Python/2.7/site-packages/peewee.py", line 1760, in iterate
return self.process_row(row)
File "/Library/Python/2.7/site-packages/peewee.py", line 1833, in process_row
setattr(instance, column, func(row[i]))
File "/Library/Python/2.7/site-packages/peewee.py", line 735, in python_value
return value if value is None else self.coerce(value)
ValueError: invalid literal for int() with base 10: ''
查看代码
@app.route('/', methods=("POST", 'GET'))
@login_required
def index():
form = GetClientsForm()
if form.validate_on_submit():
if form.sectors.data == 'Realtors':
companies = Realtor.select().where((Realtor.have_contacted != True) & (Realtor.dont_contact_anymore != True))
return render_template('new_batch.html', companies=companies)
else: #Biotech/Engineering
companies = Company.select().where((Company.have_contacted != True) & (Company.dont_contact_anymore != True))
return render_template('new_batch.html', companies=companies)
模板代码
...
{% for company in companies %}
<tr class="company-row {{ company.id }}" data-toggle="collapse" data-target="#{{ company.id }}-info" id="row-{{ company.id }}" data-company-id="{{ company.id }}">
<td class="col-md-3 company-name">
<input type="text" value="{{ company.company_name }}" id="{{ company.id }}-company-name" class="form-control" name="company-name">
</td>
<td class="col-md-2 company-website">
<div>
<a href="{{ company.website }}" target="blank_">{{ company.website }}</a>
</div>
</td>
<td class="col-md-3 company-email">
<input type="text" value="{{ company.email_address }}" id="{{ company.id }}-company-email" class="form-control" name="company-email">
</td>
<td class="col-md-1 company-contact-form">
<div class="checkbox">
<input type="checkbox" id="{{ company.id }}-contact-form" name="contact-form" data-url='contact_form_company'>
</div>
</td>
<td class="col-md-1 contact-company">
{{ render_submit('E-mail', class="contact-button", data='data-url=contact_company') }}
</td>
<td class="col-md-1 remove-company">
{{ render_submit("Don't Contact", class="dont-contact-button", data='data-url=dont_contact_company') }}
</td>
<td class="col-md-1 company-page">
<div>
<a href="{{ url_for('company_page', company_id=company.id) }}">{{ render_submit('Company Page') }}</a>
</div>
</td>
</tr>
{% endfor %}
...
更新 - 型号代码
class Company(Model):
dont_contact_anymore = BooleanField(default=False)
company_name = CharField()
website = CharField(unique=True)
email_address = CharField()
country = CharField()
scraped_on = DateTimeField(formats="%m-%d-%Y")
have_contacted = BooleanField(default=False)
current_pipeline_phase = IntegerField(default=0)
day_0_message_id = IntegerField()
day_0_contacted_by = ForeignKeyField(
rel_model=User,
related_name='contacted_by_day_0',
db_column='day_0_contacted_by'
)
day_0_emails_contacted = CharField()
day_0_response = IntegerField()
day_0_sent = DateTimeField(formats="%m-%d-%Y")
day_5_message_id = IntegerField()
day_5_contacted_by = ForeignKeyField(
rel_model=User,
related_name='contacted_by_day_5',
db_column='day_5_contacted_by'
)
day_5_emails_contacted = CharField()
day_5_response = IntegerField()
day_5_sent = DateTimeField(formats="%m-%d-%Y")
day_35_message_id = IntegerField()
day_35_contacted_by = ForeignKeyField(
rel_model=User,
related_name='contacted_by_day_35',
db_column='day_35_contacted_by'
)
day_35_emails_contacted = CharField()
day_35_response = IntegerField()
day_35_sent = DateTimeField(formats="%m-%d-%Y")
day_125_message_id = IntegerField()
day_125_contacted_by = ForeignKeyField(
rel_model=User,
related_name='contacted_by_day_125',
db_column='day_125_contacted_by'
)
day_125_emails_contacted = CharField()
day_125_response = IntegerField()
day_125_sent = DateTimeField(formats="%m-%d-%Y")
batch = IntegerField()
sector = CharField()
class Meta:
database = DATABASE
order_by = ('have_contacted',)
@classmethod
def create_company(cls, company_name, website, email_address):
try:
with DATABASE.transaction():
cls.create(company_name=company_name, website=website, email_address=email_address, scraped_on=datetime.now)
print 'Saved {}'.format(company_name)
except IntegrityError:
print '{} already exists in the database'.format(company_name)
您的模型似乎声明了 IntegerField
,但数据库中存储了一个空字符串。也许你可以去清理你的数据库:
UPDATE whatever SET int_field = NULL WHERE int_field = '';