Django,Postgres:ValueError:基数为10的int()的无效文字:''

时间:2017-03-14 18:03:34

标签: python django postgresql

我正在尝试编写代码来webscrap一些数字。如果我尝试在窗户上打印,我会把一切都搞好。打印效果很好。

但是在我添加代码以将数据保存到数据库(postgres)后,我得到了这个错误:“ValueError:int(()的无效文字,基数为10:''”

  

Models.py:

from django.db import models


class Category(models.Model):


    name = models.CharField(max_length=128, unique=False)

    def __unicode__(self):
        return '%s' %(self.name)

class Page(models.Model):
    category = models.ForeignKey(Category)
    title = models.CharField(max_length=128)
    price = models.DecimalField(decimal_places=3, max_digits=8)
    ann_hea = models.CharField(max_length=128, default='ann_headlines')
    ann_date = models.CharField(max_length=128, default ='ann_date')
    ann_det = models.CharField(max_length=128,default = 'ann_details')
    ann_ty = models.CharField(max_length=128, default='ann_type')
    multi = models.IntegerField(default=0)
    ex_da = models.CharField(max_length=128, default='date')

    def __unicode__(self):
        return self.title

=========================================

  

“views.py”

    from django.shortcuts import render
    from django.http import HttpResponse
    from django.template import RequestContext
    from django.shortcuts import render_to_response
    from django.shortcuts import render
    from rango.models import Category
    from rango.models import Page
    from rango.forms import CategoryForm

    '''
    Code for form here.......

    '''

    '''
getFeedContentCA(web):

    code to scrape web .. posting the last half for the code..
    .
    .
    .
    .
    .
        xyz = {'Title': x['Company Name'],'price': (x['Price']),'headlines':an_headlines,'Details':an_det,'type': an_ty,'multi': an_multi,'ex_da':an_ex_da,'date':an_date}

        return xyz  

y = getFeedContentCA('https://finance.yahoo.com/')
print (' _________________________________________ ')
print (y['Title'])
print (y['price'])
print (y['hea'])
print (y['Det'])
print (y['ty'])
print (y['multi'])
print (y['ex_da'])
print (y['date'])




a = Category.objects.create(name=x['Name'])

b = Page.objects.create(title=a, price=float(y['price']) ,ann_date=(y['date']), ann_hea=(y['hea']),ann_det=(y['Det']),ann_ty=(y['ty']),ex_da=(y['ex_da']), multi=(y['multi']))

b.save()

====================================

打印输出正常显示在窗口上,但是当我添加代码以将数据保存到数据库时,它会抛出该错误。我尝试在保存到数据库行的数据中添加'float'数据类型,但它没有帮助。

此处类似的错误给出了相同的添加解决方案: 1.浮点数据类型 2. Models.py中的十进制类型。

试过两个。

提前致谢!

  

添加跟踪错误:

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x0000000004A7C8C8>
Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "C:\Python34\lib\site-packages\django\core\management\commands\runserver.py", line 121, in inner_run
    self.check(display_num_errors=True)
  File "C:\Python34\lib\site-packages\django\core\management\base.py", line 374, in check
    include_deployment_checks=include_deployment_checks,
  File "C:\Python34\lib\site-packages\django\core\management\base.py", line 361, in _run_checks
    return checks.run_checks(**kwargs)
  File "C:\Python34\lib\site-packages\django\core\checks\registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:\Python34\lib\site-packages\django\core\checks\urls.py", line 14, in check_url_config
    return check_resolver(resolver)
  File "C:\Python34\lib\site-packages\django\core\checks\urls.py", line 24, in check_resolver
    for pattern in resolver.url_patterns:
  File "C:\Python34\lib\site-packages\django\utils\functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Python34\lib\site-packages\django\urls\resolvers.py", line 313, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\Python34\lib\site-packages\django\utils\functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Python34\lib\site-packages\django\urls\resolvers.py", line 306, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\Python34\lib\importlib\__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1471, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "C:\Python34\test\lepnew\urls.py", line 23, in <module>
    url(r'^rango/', include('rango.urls'))
  File "C:\Python34\lib\site-packages\django\conf\urls\__init__.py", line 50, in include
    urlconf_module = import_module(urlconf_module)
  File "C:\Python34\lib\importlib\__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1471, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "C:\Python34\test\rango\urls.py", line 18, in <module>
    from rango import views
  File "C:\Python34\test\rango\views.py", line 280, in <module>
    b = Page.objects.create(title=a, price=float(y['price']) ,ann_date=(y['ex_date']), ann_headlines=(y['headlines']),ann_details=(y['Details']),ann_type=(y['type']),ex_date=(y['ex_date']), multiplier=(y['multiplier']))
  File "C:\Python34\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Python34\lib\site-packages\django\db\models\query.py", line 399, in create
    obj.save(force_insert=True, using=self.db)
  File "C:\Python34\lib\site-packages\django\db\models\base.py", line 796, in save
    force_update=force_update, update_fields=update_fields)
  File "C:\Python34\lib\site-packages\django\db\models\base.py", line 824, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "C:\Python34\lib\site-packages\django\db\models\base.py", line 908, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "C:\Python34\lib\site-packages\django\db\models\base.py", line 947, in _do_insert
    using=using, raw=raw)
  File "C:\Python34\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Python34\lib\site-packages\django\db\models\query.py", line 1045, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "C:\Python34\lib\site-packages\django\db\models\sql\compiler.py", line 1053, in execute_sql
    for sql, params in self.as_sql():
  File "C:\Python34\lib\site-packages\django\db\models\sql\compiler.py", line 1006, in as_sql
    for obj in self.query.objs
  File "C:\Python34\lib\site-packages\django\db\models\sql\compiler.py", line 1006, in <listcomp>
    for obj in self.query.objs
  File "C:\Python34\lib\site-packages\django\db\models\sql\compiler.py", line 1005, in <listcomp>
    [self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
  File "C:\Python34\lib\site-packages\django\db\models\sql\compiler.py", line 945, in prepare_value
    value = field.get_db_prep_save(value, connection=self.connection)
  File "C:\Python34\lib\site-packages\django\db\models\fields\__init__.py", line 755, in get_db_prep_save
    prepared=False)
  File "C:\Python34\lib\site-packages\django\db\models\fields\__init__.py", line 747, in get_db_prep_value
    value = self.get_prep_value(value)
  File "C:\Python34\lib\site-packages\django\db\models\fields\__init__.py", line 1832, in get_prep_value
    return int(value)
ValueError: invalid literal for int() with base 10: ' '

1 个答案:

答案 0 :(得分:0)

DecimalField需要一个字符串。您可能希望切换到FloatField并添加帮助函数以获取格式化价格。