From 226857bc78c4c2f0aa7703632f92b94b07d5d3a0 Mon Sep 17 00:00:00 2001 From: Rafael Salimov Date: Sat, 16 Jul 2022 17:30:56 +0400 Subject: [PATCH] Prevented multiple database hits with bulk create --- quotes/management/commands/add_quotes.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/quotes/management/commands/add_quotes.py b/quotes/management/commands/add_quotes.py index d6d9b1c..f349dd1 100644 --- a/quotes/management/commands/add_quotes.py +++ b/quotes/management/commands/add_quotes.py @@ -10,7 +10,9 @@ class Command(BaseCommand): def handle(self, *args, **options): fake = Faker() - for _ in range(10000): - Quote.objects.create(name=fake.name(), quote=fake.text()) + quotes_tuple = (Quote(name=fake.name(), quote=fake.text()) + for _ in range(10_000)) + + Quote.objects.bulk_create(quotes_tuple) print("Completed!!! Check your database.")