orm的操作:

settings:

"""
Django settings for about_exam project.

Generated by 'django-admin startproject' using Django 1.11.26.

For more information on this file, see
https://docs.djangoproject.com/en/1.11/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.11/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
#定义项目根目录:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '07k_$&dsr2qjyw=*oz#bum20gtj^6k_c!#u2%7ixbad7q51zpr'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

#定义注册的app:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app01.apps.App01Config',
]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'about_exam.urls'

#定义中间件:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join ( BASE_DIR, 'templates' )]
,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

WSGI_APPLICATION = 'about_exam.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases

#定义使用默认的数据库:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}


# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]


# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/

#定义静态文件:
STATIC_URL = '/static/'
orm表数据:

 models:

 
from django.db import models

# Create your models here.

#定义班级表:
class Classes(models.Model):
c_name = models.CharField(max_length=32)
#定义班级和老师是多对多关系ManyToManyField:
teachers = models.ManyToManyField("Teacher")

#定义老师表:
class Teacher(models.Model):
t_name = models.CharField(max_length=32)
#定义性别长度字段默认是1、有选择的1代表男和2代表女:
sex = models.CharField(max_length=1,choices=(("1","男"),("2","女")))
#定义年龄IntegerField整数:
age = models.IntegerField()

#定义学生表:
class Student(models.Model):
s_name = models.CharField(max_length=32)
score = models.IntegerField()
#定义学生表多对一班级表、on_delete=models.CASCADE级联删除:
my_class = models.ForeignKey("Classes",on_delete=models.CASCADE)

猜你喜欢

转载自www.cnblogs.com/zhang-da/p/12089595.html