2017-01-06 1 views
0

파이프 라인에서 반환 된 값을 가져 오려고합니다. 나는 항복 생성기를 사용하여 아이템을 생성하고 있습니다.치료 파이프 라인에서 반환 된 값 가져 오기

그리고 이것은 내 코드입니다.

def get_or_create(model): 
    model_class = type(model) 
    created = False 

    try: 
     obj = model_class.objects.get(product_company=model.product_company, barcode=model.barcode) 
    except model_class.DoesNotExist: 
     created = True 
     obj = model # DjangoItem created a model for us. 
     obj.save() 
    return (obj, created) 


def update_model(destination, source, commit=True): 
    pk = destination.pk 
    source_dict = model_to_dict(source) 
    for (key, value) in source_dict.items(): 
     setattr(destination, key, value) 
    setattr(destination, 'pk', pk) 
    if commit: 
     destination.save() 
    return destination 
class ProductItemPipeline(object): 
    def process_item(self, item, spider): 

     if isinstance(item, ProductItem): 
      item_model = item.instance 
      model, created = get_or_create(item_model) 
      item['cover_photo'] = item['files'][0]['path'] 
      if created: 
       item.save() 
       for image in item['files']: 
        imageItem = ProductImageItem(image=image['path'], product=model) 
        imageItem.save() 
       for comment in item['comments']: 
        commentItem = CommentItem(comment=comment.comment, product=model) 
        commentItem.save() 
      return model 

또한 이것은 내 거미입니다.

item = ProductItem(name=name, price=price, barcode=barcode, file_urls=objectImages, product_url=response.url,product_company=company, comments = comments) 
     product = yield item 
     print type(product) 
     print "yield product" 

그리고 제품 유형은 nonetype

답변

0

당신은 파이썬 yield을 이해하지 못하는 반환합니다. 어떤 함수 결과를 반환하는 경우 과 같은 yield을 사용해야합니다. 당신은 발전기에 대한 자세한 정보를 찾을 수 있습니다이 article

그래서 코드 모양을

item = ProductItem(name=name, price=price, barcode=barcode, file_urls=objectImages, product_url=response.url,product_company=company, comments = comments) 
self.logger.debug(type(product)) 
self.logger.debug('yield product') 
yield item 

같은
관련 문제