Three weeks ago our development team was chatting. We were launching a new app and we have decided to manage pricing plans through the Django admin interface. Our problem was that we needed at least a FREE plan in the database to enable the user to sign up. The production environment was already set up and everything worked. But when we added the development environment, we suddenly couldn’t sign up. We have checked the logs and found out that none can sign up due to missing pricing plans. So the question was: How to populate the database with general pricing plans on deploy if they do not exist?

Custom Django command

After some research, we’ve decided to register a custom Django command and define pricing plans in the YAML file. After successful deploy command is run. So let’s take a look at how we can do that.

Basic set up

Create a new Django project called django_populate_db_with_command: startproject django_populate_db_with_command

After that, we will create a new app called pricing: startapp pricing

Now we can add our pricing plan model:

# pricing/
from django.db import models

# Create your models here.
class PricingPlan(models.Model):
    name = models.CharField('Name of plan', max_length=50, unique=True)
    amount = models.DecimalField('Price for plan', max_digits=4, decimal_places=2, default=0)

Create migrations, migrate and create superuser:

python makemigrations
python migrate
python createsuperuser

We will also register the model to admin. Therefore we will be able to see if plans were created.

# pricing/
from django.contrib import admin

# Register your models here.
from pricing.models import PricingPlan

class PricingPlanAdmin(admin.ModelAdmin):
    pass, PricingPlanAdmin)

Don’t forget to register app in

# ... other code ...
# ... other code ...

Start the development server,

python runserver

go to http://localhost:8000 and check that there are no pricing plans.

Register command

To register command into we need to create a new directory inside our pricing app called management. Inside that, we need to create a directory called commands. And inside that, we create a new python file called The structure of pricing app should look like this:

  • pricing
    • management
      • commands
    • migrations

Our should contain the following code:

# pricing/management(commands/ 
import yaml
from import BaseCommand
from pricing.models import PricingPlan

class Command(BaseCommand):
    help = 'Create pricing plans from yaml'

    def add_arguments(self, parser):
        parser.add_argument('--path', type=str)

    def handle(self, *args, **options):
        with open(options['path'], 'r') as stream:
            plans = yaml.safe_load(stream)
            for plan in plans:
                pricing_plan, _ = PricingPlan.objects.get_or_create(name=plan['name'])
                pricing_plan.amount = plan['amount']

        self.stdout.write('Successfully created plans'))

What is done here? Inside add_arguments method, an argument –path is registered to command to tell it which YAML file to use. The method called handle actually executes the command. Firstly, it reads the YAML file from provided path. Then it tries to get the existing plan with the name defined in the YAML file. If it does not exists it creates a new one. After that, it updates it with the amount defined in the YAML file.

To successfully run the command, we need to add a YAML file. Create a new YAML file inside pricing app that looks like this:

- name: Free
  amount: 0

Now we are ready to run our command:

python createpricingplans --path pricing/plans.yaml

Open http://localhost:8000 and check that our pricing plan is added now.

The code can be found here on Github.


That’s all – happy coding!

Categories: Django