如何解决如果在数据库中已经存在条件,如何在django中的views.py文件中添加条件以不创建相同的数据对象?
这是 views.py文件:-
}
我尝试了各种方法,例如在model.py文件中添加from django.shortcuts import render
from .models import News
from django.core.paginator import Paginator
from django.db.models import Q
# For scraping part
import requests
from bs4 import BeautifulSoup
def news_list(request,*args,**kwargs):
# fOR scraping part - START::::::::::::::::::::::::::::::::::::::::::::::::::::::::
response = requests.get("http://www.iitg.ac.in/home/eventsall/events")
soup = BeautifulSoup(response.content,"html.parser")
cards = soup.find_all("div",attrs={"class": "newsarea"})
iitg_title = []
iitg_date = []
iitg_link = []
for card in cards[0:6]:
iitg_date.append(card.find("div",attrs={"class": "ndate"}).text)
iitg_title.append(card.find("div",attrs={"class": "ntitle"}).text.strip())
iitg_link.append(card.find("div",attrs={"class": "ntitle"}).a['href'])
# fOR scraping part - END::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# fOR storing the scraped data directly into the dtatbase from the views.py file - START---------------------------------------------------------------
for i in range(len(iitg_title)):
News.objects.create(title = iitg_title[i],datess = iitg_date[i],linkss = iitg_link[i])
# fOR storing the scraped data directly into the dtatbase from the views.py file - END-----------------------------------------------------------------
queryset = News.objects.all() #Getting all the objects from the database
search_query = request.GET.get('q')
if search_query:
queryset = queryset.filter(
Q(title__icontains = search_query) |
Q(description__icontains = search_query)
)
paginator = Paginator(queryset,5) #Adding pagination
page_number = request.GET.get('page')
queryset = paginator.get_page(page_number)
context = {
'object_list': queryset
}
return render(request,'news_list.html',context)
,但仍然无法正常工作,会引发som等错误。我无法添加或弄清楚要添加到views.py文件中的代码的确切条件行,以便它检查数据是否已经存在。在数据库中,如果不存在,则会添加已抓取的数据,否则不会。
解决方法
我认为您可以使用get_or_create
,例如News.objects.get_or_create(title = XYZ),并使title
字段唯一。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。