Search⌘ K
AI Features

Generating the Development Data

Explore how to define data models and generate realistic development data using Faker and model_bakery. Understand how to register models with Django admin and safely populate your database for testing filters, bulk actions, and search features.

Custom admin filters, bulk actions, and search interfaces are hard to validate against an empty database. To verify how these admin changes behave, we need a realistic dataset with enough records to test edge cases.

First, we’ll define the data models, then use development data generation tools to create enough non-production data for testing so the admin customizations can be tested against realistic data.

Defining the quiz data model

Our sample application is a simple quiz system composed of three models: Author, Question, and Choice. We define these models inside the sample_app/models.py file. We use modern Python f-strings for our string representations, completely avoiding legacy string formatting patterns.

Python
# File: sample_app/models.py
from django.db import models
class Author(models.Model):
name = models.CharField(max_length=200)
created_date = models.DateTimeField(auto_now_add=True)
updated_date = models.DateTimeField(auto_now=True)
def __str__(self):
return f"{self.name}"
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
ref_author = models.ForeignKey(Author, on_delete=models.CASCADE)
created_date = models.DateTimeField(auto_now_add=True)
updated_date = models.DateTimeField(auto_now=True)
def __str__(self):
return f"[{self.ref_author.name}] : {self.question_text}"
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
created_date = models.DateTimeField(auto_now_add=True)
updated_date = models.DateTimeField(auto_now=True)
def __str__(self):
return f"{self.question.question_text} : {self.choice_text}"
  • Lines 9, 19, and 29: Implement the __str__ methods using ...