I have a Django management command, which I pass like this:
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
This doesn't work and I get an error saying that:
argument 1 must be str, not None
I suspect this is to do with the way the actual string of the date 2020-11-30 00:00:00
is being formatted. My management command code is below
generate_weekly_invoice.py
class Command(BaseCommand):
help = 'Generates weekly invoice'
def add_arguments(self, parser):
parser.add_argument('--start_date', nargs='+')
parser.add_argument('--end_date', nargs='+')
def handle(self, *args, **options):
start_str = options.get('--start_date')
end_str = options.get('--end_date')
start_date = datetime.datetime.strptime(start_str, '%Y-%m-%d %H:%M:%S')
end_date = datetime.datetime.strptime(end_str, '%Y-%m-%d %H:%M:%S')
___Logic goes here___
Any help is appreciated
*** Edit:
I should have explained, that I have passed the string in quotation marks but it still returns the same error.
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
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
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 thenargs='+'
, since that means one can pass multiple--start_date
s, etc:If you want to parse
--start_date
multiple times, then you can usenargs='+'
, but thenstart_date
andend_date
will be lists of strings, not a single string, so then you need to perform a mapping.