How to update DjangoItem in Scrapy

前端 未结 3 1063
小蘑菇
小蘑菇 2020-12-14 04:46

I\'ve been working with Scrapy but run into a bit of a problem.

DjangoItem has a save method to persist items using the Django ORM. This is

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-14 05:30

    for related models with foreignkeys

    def update_model(destination, source, commit=True):
        pk = destination.pk
    
        source_fields = fields_for_model(source)
        for key in source_fields.keys():
            setattr(destination, key, getattr(source, key))
    
        setattr(destination, 'pk', pk)
    
        if commit:
            destination.save()
    
        return destination
    

提交回复
热议问题