Django management command with arbitrary optional params

Play this article

Looking for a management command which accepts optional parameters, then you are in luck.

import logging

from import BaseCommand
from locl.locations.models import Location
from locl.locations.tasks import fetch_metrics_for_location

logger = logging.getLogger(__name__)

class Command(BaseCommand):
help = **'Fetch Location Data'

def** add\_arguments(self, parser):  
    *\# Optional argument* parser.add\_argument(**'-ids'**, **'--location\_ids'**, type=str, nargs=**'+'**, help=**'Debug location data fetch from google'**)  

**def** handle(self, \*args, \*\*kwargs):  
    location\_ids = kwargs\[**'location\_ids'**\]

This management command can be run using any of these terminal commands. I’m using location_stats as my file has this name. use your own file name in which you would place the above code

python location_stats

python location_stats -ids value1

python location_stats -ids value1 value2