django无法捕获Model DoesNot Exist错误

时间:2016-05-17 15:51:19

标签: python django

我试图在dict中缺少某个键时捕获错误,因此无法创建模型。但是,虽然引发的错误是DoesNotExist(根据回溯),但由于某些原因,下面的代码无法捕获它,我仍然得到错误: (除此之外:我如何在调试器中看到在它到达回溯之前引发了什么错误?)

try:
    fuel_type, created = FuelType.objects.get_or_create(name=tariff['serviceType'])
        if created:
            self.log_database_create(fuel_type)
except FuelType.DoesNotExist:
    fuel_type, created = FuelType.objects.get_or_create(name=service_type)
        if created:
            self.log_database_create(fuel_type)

错误:

Traceback (most recent call last):
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/models/query.py", line 465, in get_or_create
    return self.get(**lookup), False
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/models/query.py", line 387, in get
    self.model._meta.object_name
etariff.models.DoesNotExist: FuelType matching query does not exist.

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
psycopg2.IntegrityError: null value in column "name" violates not-null constraint
DETAIL:  Failing row contains (5, null).


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1531, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 938, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/Yunti/OneDrive/switcher5/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/Users/Yunti/OneDrive/switcher5/etariff/management/commands/til_scraper.py", line 35, in handle
    scraper.full_scrape(energy_usage, service_type, current_service_type, house_no, postcode, e7)
  File "/Users/Yunti/OneDrive/switcher5/etariff/management/commands/til_scraper.py", line 347, in full_scrape
    self.write_tariffs(tariff_data_list, region, service_type)
  File "/Users/Yunti/OneDrive/switcher5/etariff/management/commands/til_scraper.py", line 460, in write_tariffs
    tariff_instance = self.write_tariff(tariff, region, service_type)
  File "/Users/Yunti/OneDrive/switcher5/etariff/management/commands/til_scraper.py", line 480, in write_tariff
    fuel_type, created = FuelType.objects.get_or_create(name=tariff['serviceType'])
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/models/manager.py", line 122, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/models/query.py", line 467, in get_or_create
    return self._create_object_from_params(lookup, params)
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/models/query.py", line 507, in _create_object_from_params
    six.reraise(*exc_info)
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/utils/six.py", line 686, in reraise
    raise value
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/models/query.py", line 499, in _create_object_from_params
    obj = self.create(**params)
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/models/query.py", line 401, in create
    obj.save(force_insert=True, using=self.db)
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/models/base.py", line 708, in save
    force_update=force_update, update_fields=update_fields)
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/models/base.py", line 736, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/models/base.py", line 820, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/models/base.py", line 859, in _do_insert
    using=using, raw=raw)
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/models/manager.py", line 122, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/models/query.py", line 1039, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1060, in execute_sql
    cursor.execute(sql, params)
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/utils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: null value in column "name" violates not-null constraint
DETAIL:  Failing row contains (5, null).

0 个答案:

没有答案