37 lines
1.3 KiB
Python
37 lines
1.3 KiB
Python
from django.core.management.base import BaseCommand
|
|
from faker import Faker
|
|
from inventory.models import Customer, Dealer
|
|
|
|
class Command(BaseCommand):
|
|
help = 'Seed the Customer model with 20 records'
|
|
|
|
def handle(self, *args, **kwargs):
|
|
fake = Faker()
|
|
dealers = Dealer.objects.all()
|
|
|
|
if not dealers.exists():
|
|
self.stdout.write(self.style.ERROR('No dealers found. Please create dealers first.'))
|
|
return
|
|
|
|
for _ in range(20):
|
|
dealer = fake.random_element(elements=dealers)
|
|
first_name = fake.first_name()
|
|
middle_name = fake.first_name() if fake.boolean() else ''
|
|
last_name = fake.last_name()
|
|
email = fake.unique.email()
|
|
national_id = fake.unique.bothify(text='##########')
|
|
phone_number = fake.unique.phone_number()
|
|
address = fake.address()
|
|
|
|
Customer.objects.create(
|
|
dealer=dealer,
|
|
first_name=first_name,
|
|
middle_name=middle_name,
|
|
last_name=last_name,
|
|
email=email,
|
|
national_id=national_id,
|
|
phone_number=phone_number,
|
|
address=address
|
|
)
|
|
|
|
self.stdout.write(self.style.SUCCESS('Successfully seeded 20 customers.')) |