Python Django中model.DoesNotExist异常内部的IntegrityError

Python Django中model.DoesNotExist异常内部的IntegrityError,python,django,sqlite,Python,Django,Sqlite,我尝试将网页保存到数据库中,url应该是唯一的。如果url已经存在,我只需要在网页类和搜索类之间创建关系。但问题是,即使数据库是空的,并且操作发生在DoesNotExist异常中,我也会不断得到这个IntegrityError。请帮我解决我的问题 下面是执行保存操作的chunk views.py代码 websearch.startSearch(maxPage=2) for result in websearch.searchResult: try:

我尝试将网页保存到数据库中,url应该是唯一的。如果url已经存在,我只需要在网页类和搜索类之间创建关系。但问题是,即使数据库是空的,并且操作发生在DoesNotExist异常中,我也会不断得到这个IntegrityError。请帮我解决我的问题

下面是执行保存操作的chunk views.py代码

websearch.startSearch(maxPage=2)
    for result in websearch.searchResult:
        try:
            web = Webpage.objects.get(url=result)
            searching.objects.create(webpage=web, keyword=keyword)
        except Webpage.DoesNotExist:
            web = Webpage.objects.create(url=result)
            searching.objects.create(webpage=web, keyword=keyword)
这是网页和搜索模型

class Webpage(models.Model) :
    "webpage from search result and client's websites"
    domain = models.ForeignKey(Domain, blank=True, null=True)
    webhosting = models.ForeignKey(WebHosting, blank=True, null=True)
    url = models.URLField(max_length=250, blank=False, unique=True)
    html_page = models.TextField(blank=True, null=True)
    inspect_status = models.BooleanField(blank=True)
    scam_status =  models.BooleanField(blank=True)
    report_status =  models.BooleanField(blank=True)
    access_status =  models.BooleanField(blank=True)
    whitelist_status =  models.BooleanField(blank=True)
    compare_to = models.ForeignKey('self', blank=True, null=True)
    def __unicode__(self):
        return self.url

class searching (models.Model):
    'information on each searching activity'
    keyword = models.ForeignKey(Keyword, blank=True, null=True)
    token = models.ForeignKey(Token, blank=True, null=True)
    webpages = models.ForeignKey(Webpage)
    date = models.DateField(auto_now=True)
class comparison(models.Model):
    '''comparing website to other websites with the same event and label'''
    source = models.ForeignKey(Webpage, related_name='compare_source')
    destination = models.ForeignKey(Webpage, related_name='compare_destination')
    time = models.DateTimeField(auto_now=True)
    fuzz_ratio = models.FloatField()
    fuzz_partial_ratio = models.FloatField()
    fuzz_token_sort_ratio = models.FloatField()
    fuzz_token_set_ratio =  models.FloatField()
    difflib_ratio =  models.FloatField()
    difflib_quick_ratio =  models.FloatField()
    difflib_real_quick_ratio =  models.FloatField()
    levenshtein_ratio =  models.FloatField()
    levenshtein_seqratio =  models.FloatField()

您不需要处理异常,可以使用
get\u或\u create
内置函数

websearch.startSearch(maxPage=2)
    for result in websearch.searchResult:

         webpage, created = Webpage.objects.get_or_create(url=result)
         #webpage is a instance 
         #created is True is a new instance was created or False if it already existed. 
还有,你有一个打字错误

searching.objects.create(webpage=web, keyword=keyword)
应该是

searching.objects.create(webpages=web, keyword=keyword)

搜索
对象具有
网页
属性而不是网页,您需要添加一个
's'

似乎完整性错误并不像我所想的那样是由网页模型中的url字段引起的。它位于BooleanField类型字段中。我将网页模型更改为:

class Webpage(models.Model):
    "webpage from search result and client's websites"
    domain = models.ForeignKey(Domain, blank=True, null=True)
    webhosting = models.ForeignKey(WebHosting, blank=True, null=True)
    url = models.URLField(max_length=250, unique=True, blank=False)
    html_page = models.TextField(blank=True, null=True)
    text_body = models.TextField(blank=True, null=True)
    inspect_status = models.NullBooleanField(blank=True, null=True)
    scam_status =  models.NullBooleanField(blank=True, null=True)
    report_status =  models.NullBooleanField(blank=True, null=True)
    access_status =  models.NullBooleanField(blank=True, null=True)
    whitelist_status =  models.NullBooleanField(blank=True, null=True)
    compare_to = models.ForeignKey('self', blank=True, null=True)
    def __unicode__(self):
    return self.url

@学生:我更新了它,你有一个输入错误,你忘记了
搜索.objects.create(webpage=web,keyword=keyword)
在纠正输入错误后,我仍然得到错误。调试页面显示错误在创建网页对象中。我尝试从shell创建Webpage对象,但仍然出现相同的错误。我还是修不好。