【python学习笔记:Django】7.数据库模型浅析
Django的模型(数据库)
-
模型概述
-
在vscode下新建终端,注意python虚拟环境是否激活;
建立Django工程Week3;
在Week3下建立应用sqliteExp
-
setting.py配置
-
models.py
from django.db import models class BaseInfo(models.Model): studentId=models.CharField(max_length=50,primary_key=True) name = models.CharField(max_length=30) age = models.IntegerField() class Hobby(models.Model): hobbyId = models.IntegerField(primary_key=True) hobby=models.CharField(max_length=50) student=models.ForeignKey(BaseInfo,on_delete=models.CASCADE)
-
导入模板
-
views.py
from django.shortcuts import render,HttpResponse from sqlite3Exp.models import BaseInfo def AddStudent(request): id = request.GET['id'] name = request.GET['name'] age = request.GET['age'] info = BaseInfo(studentId=id,name=name,age=age) info.save() return HttpResponse("Adding is successful")
-
urls.py
from django.contrib import admin from django.urls import path from sqlite3Exp import views urlpatterns = [ path('admin/', admin.site.urls), path('info/',views.AddStudent), ]
一键三连呀!