AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Fake data generator python8/26/2023 ![]() write ( "Creating new data." ) # Create all the users people = for _ in range ( NUM_USERS ): person = UserFactory () people. write ( "Deleting old data." ) models = for m in models : m. atomic def handle ( self, * args, ** kwargs ): self. # setup_test_data.py import random from django.db import transaction from import BaseCommand from forum.models import User, Thread, Club, Comment from forum.factories import ( UserFactory, ThreadFactory, ClubFactory, CommentFactory ) NUM_USERS = 50 NUM_CLUBS = 10 NUM_THREADS = 12 COMMENTS_PER_THREAD = 25 USERS_PER_CLUB = 8 class Command ( BaseCommand ): help = "Generates test data". ![]() For example, for a user, you would create a factory class as follows: When using Factory Boy you create classes called "factories", which each represent a Django model. Factory Boy can easily be configured to generate random but realistic data like names, emails and paragraphs by internally using the Faker library. ![]() It's a library that's built for automated testing, but it also works well for this use-case. We'll be using Factory Boy to generate all our dummy data. ManyToManyField ( User ) Building data with Factory Boy CharField ( max_length = 128 ) member = models. Model ): """A group of users interested in the same thing""" name = models. ForeignKey ( Thread ) class Club ( models. CharField ( max_length = 128 ) poster = models. Model ): """A comment by a user on a thread""" body = models. ForeignKey ( User ) class Comment ( models. CharField ( max_length = 128 ) creator = models. Model ): """A forum comment thread""" title = models. CharField ( max_length = 128 ) class Thread ( models. Model ): """A person who uses the website""" name = models.
0 Comments
Read More
Leave a Reply. |