单表的增删改操作

本章节就直接使用上一章节创建的student

一、新增数据

  • 1、新增数据的页面

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <link href="https://cdn.bootcss.com/bootstrap/4.1.1/css/bootstrap.css" rel="stylesheet">
    </head>
    <body>
    <div class="container">
        <div class="row">
            <div class="col-md-6">
                <h1>学生页面</h1>
                <form action="" method="post">
                    <div class="form-group">
                        <label>姓名:</label>
                        <input type="text" name="name" class="form-control">
                    </div>
                    <div class="form-group">
                        <label>性别:</label>
                        <select name="gender" class="form-control">
                            <option value="男"></option>
                            <option value="女"></option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label>年龄:</label>
                        <input type="int" name="age" class="form-control">
                    </div>
                    <div class="form-group">
                        <label>手机号码:</label>
                        <input type="int" name="mobile" class="form-control">
                    </div>
                    <div class="form-group">
                        <input type="submit" value="提交" class="btn btn-primary"/>
                    </div>
                </form>
            </div>
            <div class="col-md-6"></div>
        </div>
    </div>
    </body>
    </html>
    
  • 2、获取数据插入到数据库中模型.objects.create(...)

    from django.shortcuts import render, HttpResponse
    from django.views import View
    
    # Create your views here.
    from . models import StudentModal
    
    class Student(View):
        def get(self, request):
            return render(request, 'index.html')
    
        def post(self, request):
            name = request.POST.get('name')
            gender = request.POST.get('gender')
            age = request.POST.get('age')
            mobile = request.POST.get('mobile')
            student = StudentModal.objects.create(name=name, gender=gender, age=age, mobile=mobile)
            # student.save()
            return HttpResponse('成功')
    
  • 3、也可以直接save来新增

    ...
    student = StudentModal(name=name, gender=gender, age=age, mobile=mobile)
    student.save()
    ...
    

二、修改数据

  • 方法一:采用字典的方式修改然后保存

    def test(request):
        student = StudentModal.objects.get(pk=1)
        student.name = '哈哈'
        student.save()
        return HttpResponse('ok')
    
  • 方法二:直接使用update()函数

    def test(request):
        StudentModal.objects.filter(id=2).update(name="王五")
        return HttpResponse('ok')
    
  • 说明:

    • djangopk指的就是主键id
    • update是针对QuerySet数据类型的操作,不能用在get(pk=1).update

三、删除数据

  • 1、直接使用delete()函数

    def test(request):
        result = StudentModal.objects.filter(name='王五').delete()
        print(result)
        return HttpResponse('ok')
    

四、补充说明

django开发过程中可以在settings.py中新添点配置文件来显示每次SQL执行的日志,方便与原生的SQL语句对比,如果你对原生SQL语句不熟悉可以查看

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
        },
    },
    'loggers': {
        'django.db.backends': {
            'handlers': ['console'],
            'propagate': True,
            'level': 'DEBUG',
        },
    }
}

results matching ""

    No results matching ""