Search⌘ K
AI Features

Generating the Development Data

Explore how to define data models and generate safe, realistic development data using standard tools like Faker and model_bakery. This lesson helps you populate your Django admin interface with meaningful data for testing filters, bulk actions, and interface changes effectively.

Trying to build and test custom admin filters, bulk actions, and search interfaces on an empty database is frustrating. To accurately see the impact of our architectural changes, we need a robust, realistic dataset.

We will establish our underlying data models first, and then use industry-standard tools to generate a safe volume of development data so we can begin customizing the admin interface.

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: Implements the __str__ methods using ...