Y
Y
Yura Khlyan2016-03-04 14:20:23
Django
Yura Khlyan, 2016-03-04 14:20:23

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()

==========UPD=========
And one more thing that I noticed while I'm in pdb, and in the cycle I go further through the products (I create new ones), then the previous products "of type " is, but when I exit pdb and restart the importer, the numbering continues, but the products that were added during the importer's previous launch are not found.
I will be very grateful for your help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yura Khlyan, 2016-03-04
@MAGistr_MTM

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.

D
Dmitry Voronkov, 2016-03-04
@DmitryVoronkov

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 question

Ask a Question

731 491 924 answers to any question