django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS

1、错误描述

E:\PycharmProjects\cdk\venv\Scripts\python.exe E:/PycharmProjects/cdk/venv/diango/a10.py
Traceback (most recent call last):
  File "E:/PycharmProjects/cdk/venv/diango/a10.py", line 3, in 
    class User(models.Model):
  File "E:\PycharmProjects\cdk\venv\lib\site-packages\django\db\models\base.py", line 87, in __new__
    app_config = apps.get_containing_app_config(module)
  File "E:\PycharmProjects\cdk\venv\lib\site-packages\django\apps\registry.py", line 249, in get_containing_app_config
    self.check_apps_ready()
  File "E:\PycharmProjects\cdk\venv\lib\site-packages\django\apps\registry.py", line 131, in check_apps_ready
    settings.INSTALLED_APPS
  File "E:\PycharmProjects\cdk\venv\lib\site-packages\django\conf\__init__.py", line 57, in __getattr__
    self._setup(name)
  File "E:\PycharmProjects\cdk\venv\lib\site-packages\django\conf\__init__.py", line 42, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

Process finished with exit code 1

2、错误原因

from django.db import models;

class User(models.Model):
    full_name = models.CharField(max_length=100);

    def __str__(self):
        return self.full_name;

class Student(models.Model):
    name = models.CharField(max_length=20);
    sex = models.CharField(max_length=2);
    age = models.IntegerField(max_length=2);
    user = models.ForeignKey(User,on_delete=models.CASCADE);

    def __str__(self):
        return self.name + ' ' + self.sex + ' ' + self.age;

stu = Student();
print(stu);

3、解决办法

 

你可能感兴趣的:(Python)