Python / Django处理错误UNIQUE约束

时间:2017-11-12 10:30:33

标签: python django database

美好的一天。

我正在编写一个使用Feedparser提取RSS数据的小脚本。

我得到了它,它将我需要的所有内容提取到我的数据库并存储在那里。

现在为了避免重复数据,我在模型中设置了Unique = True。

当然,现在我正在处理错误

return Database.Cursor.execute(self, query, params) sqlite3.IntegrityError: UNIQUE constraint failed: jobrss_rssjob.link

好的,不好的是它没有重复数据。但是,它完全崩溃了脚本,所以它会停止。

显然,我现在需要为错误编写一个例外 - 而这几乎是我被困的地方,因为他们也在向我抛出错误。

这是我的代码:

from __future__ import unicode_literals
import feedparser
from django.utils import timezone
import json
from django.db import IntegrityError

from jobrss.models import RSSJob

try:
        def run():

            sources = [
        "https://mybroadband.co.za/vb/external.php?type=RSS2&forumids=269",
        "http://www.bizcommunity.com/rssjobs.aspx?l=196&c=11&s=Developer&sm=1"
            ]
            data = []
            for url in sources:
                data.append(feedparser.parse(url))
                for data in data:
                    for post in data.entries:
                        title = post.title
                        link = post.link
                        add = RSSJob()
                        add.title = title
                        add.link = link
                        add.save()
                        print("Added successfully")
except IntegrityError:
    print("Data Already exists")

任何建议如何改善这一点?

哦,这些都是错误。 当我在空数据库上运行脚本时:

(techvenv) Ronalds-MacBook-Pro:TWNEW burgundy$ python manage.py runscript rsspull
Added successfully
Added successfully
Added successfully
Added successfully
Added successfully
Added successfully
Added successfully
Added successfully
Added successfully
Added successfully
Added successfully
Added successfully
Added successfully
Added successfully
Added successfully
Exception while running run() in 'scripts.rsspull'
Traceback (most recent call last):
  File "/Users/burgundy/dev/Projects/techvenv/lib/python3.6/site-packages/feedparser.py", line 398, in __getattr__
    return self.__getitem__(key)
  File "/Users/burgundy/dev/Projects/techvenv/lib/python3.6/site-packages/feedparser.py", line 356, in __getitem__
    return dict.__getitem__(self, key)
KeyError: 'append'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/burgundy/dev/Projects/techvenv/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/Users/burgundy/dev/Projects/techvenv/lib/python3.6/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/burgundy/dev/Projects/techvenv/lib/python3.6/site-packages/django_extensions/management/email_notifications.py", line 63, in run_from_argv
    super(EmailNotificationCommand, self).run_from_argv(argv)
  File "/Users/burgundy/dev/Projects/techvenv/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/burgundy/dev/Projects/techvenv/lib/python3.6/site-packages/django_extensions/management/email_notifications.py", line 75, in execute
    super(EmailNotificationCommand, self).execute(*args, **options)
  File "/Users/burgundy/dev/Projects/techvenv/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/Users/burgundy/dev/Projects/techvenv/lib/python3.6/site-packages/django_extensions/management/utils.py", line 59, in inner
    ret = func(self, *args, **kwargs)
  File "/Users/burgundy/dev/Projects/techvenv/lib/python3.6/site-packages/django_extensions/management/commands/runscript.py", line 238, in handle
    run_script(mod, *script_args)
  File "/Users/burgundy/dev/Projects/techvenv/lib/python3.6/site-packages/django_extensions/management/commands/runscript.py", line 148, in run_script
    mod.run(*script_args)
  File "/Users/burgundy/dev/Projects/TWNEW/scripts/rsspull.py", line 18, in run
    data.append(feedparser.parse(url))
  File "/Users/burgundy/dev/Projects/techvenv/lib/python3.6/site-packages/feedparser.py", line 400, in __getattr__
    raise AttributeError("object has no attribute '%s'" % key)
AttributeError: object has no attribute 'append

这是我再次运行的时候:

django.db.utils.IntegrityError: UNIQUE constraint failed: jobrss_rssjob.link

亲切的问候。 :)

1 个答案:

答案 0 :(得分:0)

您的错误是您在定义和运行该函数之前捕获了异常。您需要在try...except函数内移动run()块。

考虑一下:

>>> try:
        def run():
            print(1 / 0)
    except ZeroDivisionError:
        print "Can't divide by zero"

>>> run()
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
ZeroDivisionError: integer division or modulo by zero

它发生的原因是,当我们定义一个函数时,Python并没有实际运行该函数。因此,1 / 0在定义函数时不会引发任何错误。在这里,try...except块只会捕获函数定义中的任何错误。但是当我们调用run()函数时,我们在try...except块之外运行它。这就是我们得到错误的原因。

如何修复:

try...except块移到for函数的最内run()循环内。例如:

def run():
    for url in sources:
        data.append(feedparser.parse(url))
        for data in data:
                for post in data.entries:
                    try:
                        title = post.title
                        ...
                        print("Added successfully")
                    except IntegrityError:
                        print("Data Already exists")