Answer the question
In order to leave comments, you need to log in
Does not store the element of the class. What could be wrong?
Good day.
===========UPD 2(Solved)==========
It was necessary to just let the loop(method, because this loop is in a function, which is burnt by the @atomic decorator) complete. After completing this method, all products are saved.
I am writing a small importer (for now a sketch) for importing goods to the site from .csv, I wanted to test it and then strangeness arises. Does not save the class object. If all these operations are carried out in ./manage.py shell
, then the product is perfectly created. I tried postwatching in the debugger - nothing, it's just that the method save()
does not save the product. What could be the problem?
I use: Python 2.7 + Django 1.8 + Django-Oscar 1.1. Here is an example importer:
num_items = len(CIP)
for i, cip_code in enumerate(CIP):
self.logger.info(" - Updating product({}/{})".format(i + 1, num_items))
if cip_code:
try:
item = Product.all_objects.get(cip=cip_code)
except Product.DoesNotExist:
item = Product(cip=cip_code)
else:
item = Product()
if GTIN[i] != '':
item.gtin = GTIN[i]
if UPC[i] != '':
item.structure = 'child'
parent_product = Product.all_objects.get(upc=UPC[i])
parent_product.structure = 'parent'
parent_product.stockrecords.all().delete()
parent_product.supplyrecord_set.all().delete()
parent_product.save()
item.parent = parent_product
item.title = 'testtest'
item.save()
item.site.add(Site.objects.get(id=2))
if item.parent:
for cat in item.parent.categories.all():
ProductCategory.all_objects.get_or_create(
product=item, category=cat)
else:
try:
category = Category.all_objects.get(name='Uncategorised')
category.site.add(Site.objects.get(id=2))
category.save()
except Category.DoesNotExist:
category = create_from_breadcrumbs('Uncategorised')
category.site.add(Site.objects.get(id=2))
category.save()
ProductCategory.all_objects.get_or_create(
product=item, category=category)
import pdb; pdb.set_trace() # debug yskhlyan
item.save()
Answer the question
In order to leave comments, you need to log in
It's decided. It was just necessary to let the loop complete (the method, because this loop is in a function that is wrapped in the @atomic decorator). After completing this method, all products are saved.
Most likely you are not validating something. add after save
More:
Can be replaced by:if UPC[i]:
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question