Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python/Django处理错误唯一约束_Python_Django_Database - Fatal编程技术网

Python/Django处理错误唯一约束

Python/Django处理错误唯一约束,python,django,database,Python,Django,Database,你好 我正在编写一个使用Feedparser提取RSS数据的小脚本 我让它工作到哪里,它把我需要的一切都拉到我的数据库中,并存储在那里 现在为了避免数据被复制,我在模型中设置Unique=True 当然,现在我正在处理这个错误 返回Database.Cursor.execute(self、query、params) sqlite3.IntegrityError:唯一约束失败:jobrss\u rssjob.link 好吧,很好,它没有复制数据。但是,它完全崩溃了脚本,因此它将停止 显然,我现在

你好

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

我让它工作到哪里,它把我需要的一切都拉到我的数据库中,并存储在那里

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

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

返回Database.Cursor.execute(self、query、params)
sqlite3.IntegrityError:唯一约束失败:jobrss\u 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

(致以亲切的问候。)

您的错误是在定义和运行函数之前捕获了异常。您需要将
try…块移动到
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

非常感谢你!现在变得更有意义了。我真的很感激。祝你今天愉快。:)
>>> 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
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")