这个问题在Foreign Keys on Scrapy被问到没有接受的答案,所以我在这里用更明确的最低设置重新提出问题:
django模型:
class Article(models.Model):
title = models.CharField(max_length=255)
content = models.TextField()
category = models.ForeignKey('categories.Category', null=True, blank=True)
请注意category
的定义方式与此无关,但确实使用了ForeignKey
。所以,在django shell中,这可以工作:
c = Article(title="foo", content="bar", category_id=2)
c.save()
scrapy项目:
class BotsItem(DjangoItem):
django_model = Article
scrapy管道:
class BotsPipeline(object):
def process_item(self, item, spider):
item['category_id'] = 2
item.save()
return item
使用上面的代码,scrapy抱怨:
exceptions.KeyError: 'BotsItem does not support field: category_id'
公平,因为category_id
没有出现在django模型中,我们从中获得了scrapy项目。为了记录,如果我们有管道(假设我们有一个类别foo
):
class BotsPipeline(object):
def process_item(self, item, spider):
item['category'] = 'foo'
item.save()
return item
现在scrapy抱怨:
exceptions.TypeError: isinstance() arg 2 must be a class, type, or tuple
of classes and types
那究竟应该怎么做?
答案 0 :(得分:7)
好的,我设法解决了这个问题,我正在这里记录。正如上一个exceptions.TypeError
暗示的那样,item['category'] expects an instance of
类别class, in my case I am using [
django-categories ][1] so in the pipeline just replace with this (assume
类别已在ORM中填充:
class BotsPipeline(object):
def process_item(self, item, spider):
item['category'] = Category.objects.get(id=2)
item.save()
return item