fix: actualizacion de categorias en la importacion de productos.
This commit is contained in:
@@ -55,6 +55,14 @@ def import_products(request):
|
||||
{'form': form}
|
||||
)
|
||||
|
||||
def _categories_from_csv_string(categories_string, separator="&"):
|
||||
categories = categories_string.split(separator)
|
||||
clean_categories = [c.strip() for c in categories]
|
||||
return [_category_from_name(category) for category in clean_categories]
|
||||
|
||||
def _category_from_name(name):
|
||||
return ProductCategory.objects.get_or_create(name=name)[0]
|
||||
|
||||
def handle_import_products_file(csv_file):
|
||||
data = io.StringIO(csv_file.read().decode('utf-8'))
|
||||
reader = csv.DictReader(data, quotechar='"')
|
||||
@@ -63,15 +71,10 @@ def handle_import_products_file(csv_file):
|
||||
name=row['producto'],
|
||||
defaults={
|
||||
'price': row['precio'],
|
||||
'measuring_unit': row['unidad'],
|
||||
'measuring_unit': row['unidad']
|
||||
}
|
||||
)
|
||||
|
||||
categories = [n.strip() for n in row["categorias"].split("&")]
|
||||
categories = _categories_from_csv_string(row["categorias"])
|
||||
product.categories.clear()
|
||||
for category in categories:
|
||||
category_model, _ = ProductCategory.objects.get_or_create(
|
||||
name=category
|
||||
)
|
||||
category_model.save()
|
||||
product.categories.add(category_model)
|
||||
product.save()
|
||||
product.categories.add(category)
|
||||
|
||||
Reference in New Issue
Block a user