Fill the db with fake data

def _create_sources(fake, source_number):
    obj_list = [
        Source(
            name=fake.sentence(nb_words=10, variable_nb_words=True),
            url=fake.url(),
            isbn_10=fake.isbn10(),
            isbn_13=fake.isbn13(),
        )

        for _ in range(source_number)
    ]

    counter = 0
    for obj in obj_list:
        Source.objects.bulk_create([obj])
        print("{} {}".format(Source._meta.object_name, str(counter)))
        counter +=1

I fill my database with fake data. It is necessary for both debugging and even for development. By the way, it has nothing to do with tests.

Initially I did:

Source.objects.bulk_create(ob_list)

It worked. But it the computer kept silence for a long time. And I decided to print a notification.

My code is clumsy: I bulk_create just one object,having converted it into a list.

Could you help me find a more elegant solution?

Back to Top