DEV Community

vinod sagar
vinod sagar

Posted on

1

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

Image of Docusign

Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs