Scrapy 上的外键

问题描述 投票:0回答:2

我用 scrapy 做一个剪贴画,我在 django 上的模型是:

class Creative(models.Model):
    name = models.CharField(max_length=200)
    picture = models.CharField(max_length=200, null = True)

class Project(models.Model):
    title = models.CharField(max_length=200)
    description = models.CharField(max_length=500, null = True)
    creative = models.ForeignKey(Creative)

class Image(models.Model):
    url = models.CharField(max_length=500)
    project = models.ForeignKey(Project)

还有我的 scrapy 模型:

from scrapy.contrib.djangoitem import DjangoItem
from app.models import Project, Creative

class ProjectItems(DjangoItem):
    django_model = Project

class CreativeItems(DjangoItem):
    django_model = Creative

所以当我保存时:

creative["name"]  = hxs.select('//*[@id="owner"]/text()').extract()[0]
picture  = hxs.select('//*[@id="owner-icon"]/a/img/@src').extract()
if len(picture)>0:
    creative["picture"] = picture[0]
creative.save()


# Extract title and description of the project
project["title"] = hxs.select('//*[@id="project-title"]/text()').extract()[0]
description = hxs.select('//*[@class="project-description"]/text()').extract()
if len(description)>0:
    project["description"] = description[0]
project["creative"] = creative
project.save()

我收到错误:

Project.creative”必须是“Creative”实例。

那么,如何在scrapy上添加前键值?

django web-scraping django-models scrapy
2个回答
2
投票

这可以通过将

creative.save()
的返回值分配给
project['creative']
处的值来完成。因此,在下面的示例中,我们使用
djangoCreativeItem
变量将此信息传递给项目:

creative["name"]  = hxs.select('//*[@id="owner"]/text()').extract()[0]
picture  = hxs.select('//*[@id="owner-icon"]/a/img/@src').extract()   
if len(picture)>0:
    creative["picture"] = picture[0]
djangoCreativeItem = creative.save()

# Extract title and description of the project
project["title"] = hxs.select('//*[@id="project-title"]/text()').extract()[0]
description = hxs.select('//*[@class="project-description"]/text()').extract()
if len(description)>0:
    project["description"] = description[0]
project["creative"] = djangoCreativeItem
project.save()

1
投票

就像这里已经完成了一样,将您的广告素材的ID直接放入creative_id中,我认为它应该可以工作:

 project["creative_id"] = creative.id

它将指定外键,而不会因为对象丢失而打扰您(因为您处于 Scrapy 环境中,您不会直接接触模型对象......)。

© www.soinside.com 2019 - 2024. All rights reserved.