VScode配置 Django项目 使用应用下的tests.py测试
文件目录
tests.py
import os
import sys
from pathlib import Path
# Add base directory to PYTHONPATH
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MyshopBack.settings")
BASE_DIR = Path(__file__).resolve().parent.parent.parent
sys.path.append(os.path.join(BASE_DIR))
import django
django.setup()
from apps.basic import models
ret = models.UsersMyuser.objects.filter(id=1).first()
print(ret.last_login)
settings.py
# 一定要把项目名称放在第1位,不然会报错
sys.path.insert(0,os.path.join(BASE_DIR.parent, "MyshopBack"))
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'apps.basic',
'apps.goods',
'apps.order',
'apps.users',
]