From 03ebe62eb86fc8d5a503354f9afde916c7ec904a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Mon, 31 Aug 2020 23:04:03 +0800 Subject: [PATCH] Revised the accounting_sample command to try to use the user with the log in ID as the current system user when there are more than one users in the accounting application. --- accounting/management/commands/accounting_sample.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/accounting/management/commands/accounting_sample.py b/accounting/management/commands/accounting_sample.py index b819daa..32d1397 100644 --- a/accounting/management/commands/accounting_sample.py +++ b/accounting/management/commands/accounting_sample.py @@ -19,6 +19,7 @@ """ import datetime +import getpass import random from typing import Optional @@ -75,8 +76,14 @@ class Command(BaseCommand): elif user_model.objects.count() == 1: user = user_model.objects.first() else: - error = "Please specify the user with -u." - raise CommandError(error, returncode=1) + try: + user = user_model.objects.get(**{ + user_model.USERNAME_FIELD: getpass.getuser() + }) + except ObjectDoesNotExist: + error = "Please specify the user with -u." + raise CommandError(error, returncode=1) + self.stdout.write(F"Filling sample data as \"{user}\"") with transaction.atomic(): self._filler = DataFiller(user)