Creating Dummy Data
Introduction
I was trying to figure out way to create bunch of dummy data for testing that can be done automatically instead of manually.
Solution
I ended up finding out django-seed (opens in a new tab) package that allows me to create dummy data as much as I want.
I referred to How to create custom django-admin
commands (opens in a new tab)
and tried to create custom command with using that package.
Process
Installing Package
Install django-seed package.
pip install django-seed
Configuring INSTALLED_APPS
Install (opens in a new tab) package in INSTALLED_APPS
of settings.py
.
# settings.pyINSTALLED_APPS = [ # ... "django_seed", # ...]
Create File
Create file for custom command creating user dummy data. I followed exactly what Django suggested (opens in a new tab).
- __init__.py
- __init__.py
- fake_users.py
Creating Custom Command Class
Create Command
class inherited from BaseCommand
(opens in a new tab).
django_seed
Initialization
Initialize seeder
Provider in order to utilize django-seed pacakge.
Help Text
I used help
(opens in a new tab) attribute for adding help text of command.
Designating Command Argument
I designated argument that can be passed to my custom command with using add_arguments()
(opens in a new tab) method.
I created argument called --number
and set its default value as 1
.
Command Logic
Create command logic with using handle()
(opens in a new tab) method.
First, bring --number
argument that is passed to our custom command.
Seeder Configuration
After importing User
model set in models.py
, I specified which values should be filled in each field1.
Editing Success Message
Lastly, I customized style of success syntax message with using style
(opens in a new tab) property.
I passed number
argument value with using f-string.
Creating Custom Command Class
Create Command
class inherited from BaseCommand
(opens in a new tab).
Help Text
I used help
(opens in a new tab) attribute for adding help text of command.
Designating Command Argument
I designated argument that can be passed to my custom command with using add_arguments()
(opens in a new tab) method.
I created argument called --number
and set its default value as 1
.
Command Logic
Create command logic with using handle()
(opens in a new tab) method.
First, bring --number
argument that is passed to our custom command.
Seeder Configuration
After importing User
model set in models.py
, I specified which values should be filled in each field1.
Editing Success Message
Lastly, I customized style of success syntax message with using style
(opens in a new tab) property.
I passed number
argument value with using f-string.
Result
It works like a charm as shown in image below.
Debugging
TypeError: make_aware() got an unexpected keyword argument 'is_dst'
Problem
I tried to seed fake dummy data with using following command line in terminal using django-seed package.
python manage.py seed users --number=10
But then I got the following error.
Traceback (most recent call last): File "/Users/apensia/dev/knower-admin/manage.py", line 32, in <module> main() File "/Users/apensia/dev/knower-admin/manage.py", line 28, in main execute_from_command_line(sys.argv) File "/Users/apensia/dev/knower-admin/venv/lib/python3.11/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line utility.execute() File "/Users/apensia/dev/knower-admin/venv/lib/python3.11/site-packages/django/core/management/__init__.py", line 436, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/Users/apensia/dev/knower-admin/venv/lib/python3.11/site-packages/django/core/management/base.py", line 413, in run_from_argv self.execute(*args, **cmd_options) File "/Users/apensia/dev/knower-admin/venv/lib/python3.11/site-packages/django/core/management/base.py", line 459, in execute output = self.handle(*args, **options) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/apensia/dev/knower-admin/venv/lib/python3.11/site-packages/django/core/management/base.py", line 646, in handle app_output = self.handle_app_config(app_config, **options) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/apensia/dev/knower-admin/venv/lib/python3.11/site-packages/django_seed/management/commands/seed.py", line 63, in handle_app_config generated = seeder.execute() ^^^^^^^^^^^^^^^^ File "/Users/apensia/dev/knower-admin/venv/lib/python3.11/site-packages/django_seed/seeder.py", line 250, in execute executed_entity = entity.execute(using, inserted_entities) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/apensia/dev/knower-admin/venv/lib/python3.11/site-packages/django_seed/seeder.py", line 157, in execute faker_data = { ^ File "/Users/apensia/dev/knower-admin/venv/lib/python3.11/site-packages/django_seed/seeder.py", line 158, in <dictcomp> field: format_field(field_format, inserted_entities) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/apensia/dev/knower-admin/venv/lib/python3.11/site-packages/django_seed/seeder.py", line 144, in format_field return format(inserted_entities) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/apensia/dev/knower-admin/venv/lib/python3.11/site-packages/django_seed/guessers.py", line 120, in <lambda> return lambda x: _timezone_format(faker.date_time()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/apensia/dev/knower-admin/venv/lib/python3.11/site-packages/django_seed/guessers.py", line 21, in _timezone_format return timezone.make_aware(value, timezone.get_current_timezone(), is_dst=False) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^TypeError: make_aware() got an unexpected keyword argument 'is_dst'
Analysis
While spectating issues created in django-seed Github repositories, other developers also experienced same error while creating DateTime data in Django version 5 or more.
This is because Django version 5 or later does not accept is_dst
as an argument anymore.
Solution
According to salimbyte's suggestion, (opens in a new tab), I solved the problem by setting
USE_TZ
(opens in a new tab) value as False
.
# settings.pyUSE_TZ = False