外来冲浪的钥匙 [英] Foreign Keys on Scrapy
本文介绍了外来冲浪的钥匙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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()
如果len(图片)> 0:
creative [picture] =图片[0]
creative.save )
#提取项目的标题和描述
project [title] = hxs.select('// * [@ id =project-title] /text()').extract()[0]
description = hxs.select('// * [@ class =project-description] / text()')extract()
如果len(description)> 0:
project [description] = description [0]
project [creative] = creative
project.save()
我收到错误:
Project.creative必须是一个创意实例。
那么,我如何在scrapy上添加一个前缀键值?
解决方案
可以通过分配 creative.save()$的返回值来完成c $ c>到
project ['creative'] $的值c $ c>所以例如在下面的例子中,我们使用
djangoCreativeItem
变量将这些信息传递给项目:
creative [name] = hxs.select('// * [@ id =owner] / text()')extract()[0]
图片= hxs.select('// * [@ id =owner-icon] / a / img / @ src')extract()
如果len(图片)> 0:
creative [picture] =图片[0]
djangoCreativeItem = creative.save()
#提取项目的标题和描述
project [title] = hxs.select('// * [@ id =project-title] / text()')extract()[0]
description = hxs.select('// * [@ class =项目描述] / text()')。extract()
如果len(description)> 0:
project [description] = description [0]
project [创意] = djangoCreativeItem
project.save()
im doing an scrap with scrapy and my model on django is:
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)
And my scrapy model:
from scrapy.contrib.djangoitem import DjangoItem
from app.models import Project, Creative
class ProjectItems(DjangoItem):
django_model = Project
class CreativeItems(DjangoItem):
django_model = Creative
So when i save:
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()
I got the error:
Project.creative" must be a "Creative" instance.
So, how can i add a foreing key value on scrapy?
解决方案
This can be done by assigning the return value of the creative.save()
to the value at project['creative']
So for instance in the following example we use the djangoCreativeItem
variable to pass this information to the project:
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()
这篇关于外来冲浪的钥匙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文