带有外键的scrapy djangoitem [英] scrapy djangoitem with Foreign Key

查看:165
本文介绍了带有外键的scrapy djangoitem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里 Scrapy上的外键被问到这个问题,但我没有被接受此处使用更明确的最小设置重新提出问题:

This question was asked here Foreign Keys on Scrapy without an accepted answer, so I am here to re-raise the question with a clearer defined minimum set up:

django模型:

class Article(models.Model):
    title = models.CharField(max_length=255)
    content = models.TextField()
    category = models.ForeignKey('categories.Category', null=True, blank=True)

请注意<$ c $这里定义的c>类别无关紧要,但是它确实使用了 ForeignKey 。因此,在django shell中,这将起作用:

Note how category is defined is irrelevant here, but it does use ForeignKey. So, in django shell, this would work:

c = Article(title="foo", content="bar", category_id=2)
c.save()

易碎物品:

class BotsItem(DjangoItem):
    django_model = Article

刮擦管道:

class BotsPipeline(object):
    def process_item(self, item, spider):
        item['category_id'] = 2
        item.save()
        return item

使用上面的代码,scrapy抱怨:

With the above code, scrapy complains:

exceptions.KeyError: 'BotsItem does not support field: category_id'

一般,因为 category_id 并没有出现在Django模型中,我们可以从中获得刮擦的物品。作为记录,如果我们有管道(假设我们有一个类别 foo ):

Fair, since category_id is not appeared in django model, from which we get the scrapy item. For the record, if we have the pipeline (assume we have a category foo):

class BotsPipeline(object):
    def process_item(self, item, spider):
        item['category'] = 'foo'
        item.save()
        return item

现在沙哑的抱怨:

exceptions.TypeError: isinstance() arg 2 must be a class, type, or tuple
 of classes and types

那么我们到底应该怎么做?

So exactly what should we do?

推荐答案

好的设法解决了这个问题,我在这里作记录。正如最近的 exceptions.TypeError 所暗示的那样, item ['category']期望一个 category <$ c $的实例。 c> class,在我的情况下,我使用的是[ django-categories ] [1],因此在管道中只需将其替换(假设 Category`已在ORM中填充):

Okay I managed to solve this problem and I am putting here for the records. As hinted by the last exceptions.TypeError, item['category'] expects an instance ofcategoryclass, in my case I am using [django-categories][1] so in the pipeline just replace with this (assumeCategory` is populated in ORM already):

class BotsPipeline(object):
    def process_item(self, item, spider):
        item['category'] = Category.objects.get(id=2)
        item.save()
        return item

这篇关于带有外键的scrapy djangoitem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆