我尝试将网页保存到数据库,网址应该是唯一的。如果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()
答案 0 :(得分:1)
您不需要处理异常,您可以使用get_or_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)
Searching
对象的webpages
属性不是网页,您需要添加's'
答案 1 :(得分:0)
似乎IntegrityError不是由我认为的网页模型中的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