Get or Create
Most of your code seems to deal with the obvious weakness of get_or_create
# Normally, we would use `get_or_create`. However, `get_or_create` would
Fortunately, this obvious short approximation can be easily overcome. Thanks to the default parameter get_or_create
Any keyword arguments passed to get_or_create () - with the exception of an optional one called defaults - will be used in the get () call. If the object is found, get_or_create () returns a tuple of this object and False. If multiple objects are found, get_or_create raises MultipleObjectsReturned. If the object is not found, get_or_create () will create and save the new object, returning a tuple of the new object and True.
Update or Create
Still not convinced that get_or_create is the right person to work with? So do I. There is something even better. update_or_create !!
A convenient method for updating an object with kwargs data, creating a new one if necessary. By default, the dictionary (field, value) used to update the object is used.
But I will not dwell on the user update_or_create, because the lines in your code that are trying to update your model have been commented out and you have not clearly indicated that you want to update.
New conveyor
Using standard API methods, your module containing your pipeline simply boils down to the ProductItemPipeline class. And it can be changed
class ProductItemPipeline(object): def process_item(self, item, spider): if isinstance(item, ProductItem): item['cover_photo'] = item['files'][0]['path'] model, created = ProductItem.get_or_create(product_company=item['product_company'], barcode=item['bar_code'], defaults={'Other_field1': value1, 'Other_field2': value2}) if created: for image in item['files']: imageItem = ProductImageItem(image=image['path'], product=item.instance) imageItem.save() return item if isinstance(item, CommentItem): model, created = CommentItem.get_or_create(field1=value1, defaults={ other fields go in here'}) if created: print model else: print created return item
Error in source code
I believe this is the place where there was a mistake.
obj = model_class.objects.get(product=model.product, comment=model.comment)
Now we are not using this, so the error should go away. If you still have problems, insert the full trace.