[Django]-Passing in date string to Django management argument

4πŸ‘

βœ…

This is because a space is seen as a parameter separator. You can wrap the parameters over single/double quotes, so --start_date '2020-11-30 00:00:00' instead of --start_date 2020-12-06 23:59:00:

python manage.py tenant_command generate_weekly_invoice --start_date '2020-11-30 00:00:00' --end_date '2020-12-06 23:59:00' --schema=schema_name
#                                      pass the datetimes as a single parameter ↑                                ↑

This is because a space is seen as a parameter separator. You can wrap the parameters over single/double quotes, so --start_date '2020-11-30 00:00:00' instead of --start_date 2020-12-06 23:59:00:

python manage.py tenant_command generate_weekly_invoice --start_date '2020-11-30 00:00:00' --end_date '2020-12-06 23:59:00' --schema=schema_name
#                                      pass the datetimes as a single parameter ↑                                ↑

additionally, your misunderstood how parameters are handled. This is without the double hyphens. You can thus use options.get('start_date'), but it is better to simply use the names of the parameters, and drop the nargs='+', since that means one can pass multiple --start_dates, etc:

class Command(BaseCommand):
    help = 'Generates weekly invoice'

    def add_arguments(self, parser):
        parser.add_argument('--start_date')  # ← no nargs='+'
        parser.add_argument('--end_date')    # ← no nargs='+'
    
    def handle(self, start_date, end_date, **kwargs):
        start_date = datetime.datetime.strptime(start_date, '%Y-%m-%d %H:%M:%S')
        end_date = datetime.datetime.strptime(end_date, '%Y-%m-%d %H:%M:%S')

If you want to parse --start_date multiple times, then you can use nargs='+', but then start_date and end_date will be lists of strings, not a single string, so then you need to perform a mapping.

Leave a comment