Tuesday, 15 January 2013

python - scrapy djangoitem with Foreign Key -



python - scrapy djangoitem with Foreign Key -

this question asked here foreign keys on scrapy without accepted answer, here re-raise question clearer defined minimum set up:

the django model:

class article(models.model): title = models.charfield(max_length=255) content = models.textfield() category = models.foreignkey('categories.category', null=true, blank=true)

note how category defined irrelevant here, utilize foreignkey. so, in django shell, work:

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

the scrapy item:

class botsitem(djangoitem): django_model = article

the scrapy pipeline:

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

with above code, scrapy complains:

exceptions.keyerror: 'botsitem not back upwards field: category_id'

fair, since category_id not appeared in django model, scrapy item. record, if have pipeline (assume have category foo):

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

now scrapy complains:

exceptions.typeerror: isinstance() arg 2 must class, type, or tuple of classes , types

so should do?

okay managed solve problem , putting here records. hinted lastly exceptions.typeerror, item['category'] expects instance ofcategoryclass, in case using [django-categories][1] in pipeline replace (assumecategory` populated in orm already):

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

python django scrapy

No comments:

Post a Comment