class Command(BaseCommand):
help = 'Create a catalog, accept csv as argument'
def add_arguments(self, parser):
parser.add_argument('file', nargs='+', type=str)
parser.add_argument('map', nargs='+', type=str)
def handle(self, *args, **options):
map_dict = mapping(options['map'][0])
with open(options['file'][0], 'r') as f:
reader = csv.DictReader(f, delimiter=';')
for row in reader:
x = Catalog.objects.get_or_create(brand=row['brand'], supplier=row['supplier'],
catalog_code=row['catalog_code'],
collection=map_dict['collection'][row['collection']],
season=map_dict['season'][row['season']],
size_group=map_dict['size_group_code'][row['size_group_code']],
currency=row['currency'], target_area=row['target_area'])
if x[1]:
logger_catalog.info(f'Created Catalog instance {x[0]}')
y = Article.objects.get_or_create(article_structure=map_dict['article_structure_code'][row['article_structure_code']],
article_number=row['article_number'], catalog=x[0])
if y[1]:
logger_catalog.info(f'Created Article instance {y[0]}')
z = Variation.objects.get_or_create(ean=row['ean'], article=y[0], size_code=row['size_code'],
color=map_dict['color_code'][row['color_code']],
material=row['material'], price_buy_gross=row['price_buy_gross'],
price_buy_net=row['price_buy_net'],
discount_rate=row['discount_rate'], price_sell=row['price_sell'],
size=map_dict['size_group_code|size_code'][f"{row['size_group_code']}|{row['size_code']}"])
if z[1]:
logger_catalog.info(f'Created Variation instance {z[0]}')
Finally I remake my Command, now it's indipendent from indeces so it will correctly populate the database even if the colums in the csv are in a different order.