Skip to main content
remove edit scar
Source Link
Vogel612
  • 25.5k
  • 7
  • 59
  • 141

EDIT:

My mapping() func (see question) returns a nested dict, the keys of the parent dict are the columns names that need to be mapped and the values are dicts with this structure:   
{value_presented_in_csv: how_value_should_be_presented_in_DB}. In

In my Command I iterate through each row of pricat.csv turning rows in dicts {colum_name: value_presented_in_csv}, if the data don't need to be mapped I get the value from my row dict like brand=row['brand'], if the data need to be mapped I get the value from my nested dict map_dict like this map_dict[column_name][value_presented_in_csv] (this gives me the value of the child dict that is how_value_should_be_presented_in_DB).

EDIT:

My mapping() func (see question) returns a nested dict, the keys of the parent dict are the columns names that need to be mapped and the values are dicts with this structure:  {value_presented_in_csv: how_value_should_be_presented_in_DB}. In my Command I iterate through each row of pricat.csv turning rows in dicts {colum_name: value_presented_in_csv}, if the data don't need to be mapped I get the value from my row dict like brand=row['brand'], if the data need to be mapped I get the value from my nested dict map_dict like this map_dict[column_name][value_presented_in_csv] (this gives me the value of the child dict that is how_value_should_be_presented_in_DB).

My mapping() func (see question) returns a nested dict, the keys of the parent dict are the columns names that need to be mapped and the values are dicts with this structure: 
{value_presented_in_csv: how_value_should_be_presented_in_DB}.

In my Command I iterate through each row of pricat.csv turning rows in dicts {colum_name: value_presented_in_csv}, if the data don't need to be mapped I get the value from my row dict like brand=row['brand'], if the data need to be mapped I get the value from my nested dict map_dict like this map_dict[column_name][value_presented_in_csv] (this gives me the value of the child dict that is how_value_should_be_presented_in_DB).

added clarity
Source Link

EDIT:

My mapping() func (see question) returns a nested dict, the keys of the parent dict are the columns names that need to be mapped and the values are dicts with this structure: {value_presented_in_csv: how_value_should_be_presented_in_DB}. In my Command I iterate through each row of pricat.csv turning rows in dicts {colum_name: value_presented_in_csv}, if the data don't need to be mapped I get the value from my row dict like brand=row['brand'], if the data need to be mapped I get the value from my nested dict map_dict like this map_dict[column_name][value_presented_in_csv] (this gives me the value of the child dict that is how_value_should_be_presented_in_DB).

It is better because doesn't relies on indeces no more, my first implementation works correctly only if the columns in pricat.csv are in that precise order; with this new implementation the columns can be in any order and the DB would still be populate correctly.

EDIT:

My mapping() func (see question) returns a nested dict, the keys of the parent dict are the columns names that need to be mapped and the values are dicts with this structure: {value_presented_in_csv: how_value_should_be_presented_in_DB}. In my Command I iterate through each row of pricat.csv turning rows in dicts {colum_name: value_presented_in_csv}, if the data don't need to be mapped I get the value from my row dict like brand=row['brand'], if the data need to be mapped I get the value from my nested dict map_dict like this map_dict[column_name][value_presented_in_csv] (this gives me the value of the child dict that is how_value_should_be_presented_in_DB).

It is better because doesn't relies on indeces no more, my first implementation works correctly only if the columns in pricat.csv are in that precise order; with this new implementation the columns can be in any order and the DB would still be populate correctly.

Source Link

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.