DEV Community

vinod sagar
vinod sagar

Posted on

Using Faker Module in Django

When ever we are working on some application and testing on it , we need some data which are harder to type often so using this faker data module , we can save time and helpful to do productive work.

pip install Faker

Below is the documentation of FAKER module
[https://faker.readthedocs.io/en/master/]

This is the code we can use in Django application.

create a file in project folder any name faker_data.py

import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'crudproject.settings') # crudproject is your project name 
import django
django.setup()
import random
from crudapp.models import Student #this is application name and model
from faker import Faker
fake = Faker()
country = ['India', 'Sweden', 'England', 'Italy', 'Brazil', 'Austria'] 

def lazy_name():
  fname = fake.first_name()
  lname = fake.last_name()
  email = fake.email()
  mobile = fake.ean(length=13)
  location = random.choice(country)
  student = Student.objects.get_or_create(fname=fname, lname=lname, email=email, mobile=mobile, location=location)
  return student

def generate_times(N):
  for _ in range(N):
  lazy_name()

if __name__ == '__main__':
print("Generating please wait..")
generate_times(10)
print("Names generated")
Enter fullscreen mode Exit fullscreen mode

Thank you

Top comments (0)