When working with django, it was necessary to write a script to populate the database for further testing.

There is a table in which it is necessary to refer to another (previously filled) table, for this I remove all id records from the filled table using the function:

 def listId(Entity): list_id = Entity.objects.values_list('id') if not list_id: exit(0) return list_id 

Where Entity is an entity (model) corresponding to this table, but when list_id is displayed on the screen:

QuerySet [(10919745,), (10919746,), (10919747,), (10919748,), (10919749,), (10919750,), (10919751,), (10919752,), (10919753,), (10919754, )]

This affects 2 questions:

  1. Why are there such strange id in the table?

data generation for this table goes to functions:

 def generateWModel(N): print("\nWModel\n") roll = [] personal = e.Personal('is') for i in range(int(N)): roll.append(WModel( wname = personal.name(gender = 'male') )) print(roll[i - 1]) return roll 

and is entered into the table by the function:

 def commit(data, Entity): try: mess = Entity.objects.bulk_create(data) print(mess) except Exception as ex: print(ex) return 

At the moment there are no more than 50 records

  1. Why the list_id element list_id so weird, with a comma at the end

10919745,

    1 answer 1

    1. We send the base and, when generating, explicitly set the id new record:

     def generateWModel(N): print("\nWModel\n") roll = [] try: n = WModel.objects.last().id except Exception as i: print(i) n = 0 print(n) personal = e.Personal('is') for i in range(int(N)): roll.append(WModel( pk = i + n + 1, wname = personal.name(gender = 'male') )) print(roll[i - 1]) return roll 

    To begin with, with the help of n = WModel.objects.last().id out the id last record, then with the help of pk = i + n + 1 set the id new record to the base

    1. Use list_id = Entity.objects.values_list('id', flat=True)