【问题标题】:Django is not updating my model objects instead they created newDjango 没有更新我的模型对象,而是创建了新的
【发布时间】:2020-08-11 17:43:47
【问题描述】:

所以这是我运行良好的简单功能。基本上它是从这个站点提取数据。

def mega():
    pageNumber = 1
    nextPage = True
    proList = []
    while nextPage:
        url = "http://www.mega.pk/mobiles/{}/".format(pageNumber)
        response = get(url)
        soup = BeautifulSoup(response.text, features="lxml")
        ul = soup.find('ul', class_='item_grid list-inline clearfix')
        li = ul.find_all('li', class_='col-xs-6')
        if not li:
            break
        for link in li:
            if link.find("div", class_="was"):
                title = link.find(id="lap_name_div").text.replace("\n", "")
                data = link.find("div", class_="cat_price").text.replace(
                    "\n", "").replace("\t", "").replace(" ", "")
                lists = data.split("-")
                if len(lists) > 2:
                    price = lists[1].replace("PKR", "") + "-PKR"
                else:
                    price = data
                price = int(price.replace(",", "").replace(
                    '-', "").replace("PKR", ""))
                productUrl = link.find("a")['href']
                image = link.find("img")['data-original']
                pro = Product(title=title, price=price, productUrl=productUrl,imageUrl=image, site="mega.pk")
                proList.append(pro)    
        pageNumber = pageNumber + 1
    return proList

但以下是我尝试分配 item.price = product.price 时的观点。然后不是更新它而是创建新实例。

def scraper(request):
    products = mega()
    proList = Product.objects.all()
    for product in products:
        for item in proList:
            if product.title == item.title and product.price == item.price:
                break
            elif product.title == item.title and product.price != item.price:
                notify = Notification(user = request.user, changeMessage="Price updated")
                notify.save()
                item.price = product.price
                break

    return redirect("home")

像这样 enter image description here

【问题讨论】:

    标签: python-3.x django django-rest-framework django-views python-requests


    【解决方案1】:

    因为您使用 proList = Product.objects.all() 从数据库中获取项目,然后使用 for product in products: 对其进行迭代

    您应该将其从 item.price = product.price 更改为:

    product.price = item.price
    product.save() 
    

    然后您将使用从mega() 获得的新项目更新数据库中的现有记录

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-13
      • 2015-07-26
      • 2018-05-09
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多