[Solved]-'dict' object has no attribute 'pk' when using Django bulk_create() function

10👍

You obtain the value in a dictionary for the corresponding key by subscripting, like:

objs = [CounterFileData(date=row['date'], value=row['value']) for row in parsed_data]

Furthermore you passed parsed_data to the list(..) constructor, whereas it should be objs. By using list comprehension however, we can omit that.

batch = [CounterFileData(date=row['date'], value=row['value']) for row in parsed_data]
CounterFileData.objects.bulk_create(batch)

Leave a comment