Django при запуске сервера ModuleNotFoundError: Нет модуля с именем 'customer data'
Я использую django для создания API, который соединяется с другим API для получения некоторых данных и обновления некоторых значений. Идея заключается в том, что api может выполняться из CLI с помощью команд типа upgrade customer_id target_subscription
.
До создания CLI-файла api прекрасно работает, если делать к нему запросы (например, используя thunder-клиент vs code). После создания CLI-файла и попытки тестирования я получаю следующую ошибку:
Я запускаю код на Windows 10 и WSL bash. Я создаю проект django, используя:
django-admin startproject customer_data
cd customer_data
python manage.py startapp customer
Структура моих файлов/папок такова:
Мой код API:
class UpdateCustomerSubscriptionView(APIView):
def extractCustomerData(self, customer_id):
"""
Input: A customer id
Output: The customer data
"""
customer = None
try:
response = requests.get(f'{API_LINK}{customer_id}/')
customer_data = response.json()
customer = Customer(**customer_data)
except:
return Response({"error": "The ID does not exist or is badly formated."}, status=500)
return customer
def upgrade_or_downgrade(self, customer_id, target_subscription):
"""
Input: A customer id and a target subscription level
Output: The updated customer data
"""
# Validate that the id is not null
if customer_id is None:
return Response({"error": "The customer id is null."}, status=500)
# Validate that the new subscription is not null
if target_subscription is None:
return Response({"error": "The target subscriptiond is null."}, status=500)
# Validate that the new subscription is valid.
if target_subscription not in SUBSCRIPTION_CHOICES:
return Response({"error": "The target subscription level is not reachable."}, status=500)
# Get customer data from the external API
customer = self.extractCustomerData(customer_id)
if isinstance(customer, Response):
return customer
# Update the customer's subscription level
customer.data['SUBSCRIPTION'] = target_subscription
customer_data = model_to_dict(customer)
customer_data['id'] = customer.id
data_json = json.dumps(customer_data)
headers = {'content-type': 'application/json'}
# Making the PUT request to customerdataapi
r = requests.put(f'{API_LINK}{customer_id}/', data=data_json, headers=headers)
print(r.status_code)
return Response(customer_data, status=200)
def post(self, request):
# Get customer id and target subscription level from the request body
customer_id, target_subscription = sys.argv[1], sys.argv[2]
print(customer_id, target_subscription)
return self.upgrade_or_downgrade(customer_id, target_subscription)
Мой файл CLI выглядит следующим образом:
if [ "setup" == $1 ]; then
echo "Running setup"
# Replace this with any commands you need to setup your code
# pip install -r requirements.txt
exit;
fi
if [ "upgrade" == $1 ]; then
echo "Upgrading with args: ${@:2}"
# Use this line to call your code
export DJANGO_SETTINGS_MODULE="customer_data.settings"
python customer_data/customer/views.py upgrade ${@:2} || python3 customer_data/customer/views.py upgrade ${@:2}
exit;
fi
if [ "downgrade" == $1 ]; then
echo "Downgrading with args: ${@:2}"
# Use this line to call your code
export DJANGO_SETTINGS_MODULE="customer_data.settings"
python customer_data/customer/views.py downgrade ${@:2} || python3 customer_data/customer/views.py downgrade ${@:2}
exit;
fi
echo "Your first argument must be either 'setup', 'upgrade' or 'downgrade'"
exit 5;
Я пробую this, но ничего не получается. В my setting.py
файле в INSTALLED_APPS
я добавляю 'customer' и 'rest_framework'. Заранее благодарю за помощь, любая другая информация, которая вам нужна, пожалуйста, дайте мне знать.