Answer the question
In order to leave comments, you need to log in
Scrapy + django-item + postgresql (sqlalchemy), how to map a class?
I use this stack in a project. The following error is thrown:
sqlalchemy.orm.exc.UnmappedInstanceError: Class news_scrap.items.NewsItem' is not mapped.
from scrapy_djangoitem import DjangoItem
from main.models import News
class NewsItem(DjangoItem):
django_model = News
__tablename__ = "main_news"
from sqlalchemy import *
from sqlalchemy.engine.url import URL
from . import settings
def db_connect():
"""
Performs database connection using database settings from settings.py.
Returns sqlalchemy engine instance
"""
return create_engine(URL(**settings.DATABASE))
from sqlalchemy.orm import sessionmaker
from .items import NewsItem
from .models import db_connect
class NewsScrapPipeline(object):
def __init__(self):
"""
Initializes database connection and sessionmaker.
Creates deals table.
"""
#log.INFO('First Point')
engine = db_connect()
self.Session = sessionmaker(bind=engine)
def process_item(self, item, spider):
session = self.Session()
news = NewsItem(**item)
#log.INFO('This Point')
try:
session.add(news)
session.commit()
except:
session.rollback()
raise
finally:
session.close()
return item
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question