Python3:修改前面的Django应用的增删改查(使用当前django内置的models实现)

1.前言

由于前面实现了Django对数据库的增删改查(手动创建连接并使用sql方式创建),这里对前面的增删改查操作做优化,实现使用models对当前的数据增删改查操作(这里的models为django.db中的models)

前面的例子:Python3:初次使用Django创建web应用并实现增删改查(使用sqlite3作为数据库)

2.执行数据迁移操作

2.1 迁移实体类

1.将对应的user.py这个类迁移到对应的webapp中的models.py中
在这里插入图片描述

其中models.py中的内容为


# 使用当前的数据数据模型实现增删改

class User(models.Model):
    username = models.CharField(max_length=50)
    password = models.CharField(max_length=50)

    def __str__(self):
        return "User[id={0},username={1},password={2}]".format(self.id, self.username, self.password)

    class Meta:
        db_table = "users"

这里需要主义的是,由于当前的表名为:users,所以这里需要使用class Meta: db_table = "users"方式重新定义表明,如果不重写就会导致访问数据库失败

2.2 迁移userController类

由于这里是基本的测试,我就将原来的代码放入到webapp中的views.py中,然后重新修改userController中的内容,(但是实际上的操作就是直接再views.py中重新写入数据,而userController需要删除,本人觉得麻烦就直接这样了)

2.3 重写userController类中的实现的方法

from django.shortcuts import render
from django.shortcuts import redirect
from webapp.models import User

def to_add_user_page(request):
    print("visit to addUser page")
    return render(request, "adduser.html")


# 重定向需要使用当前的redirect模块来实现定向操作、
def add_user(request):
    request.encoding = 'utf-8'
    if 'username' in request.POST and request.POST['username']:
        username = request.POST["username"]
    if 'password' in request.POST and request.POST['password']:
        password = request.POST["password"]
    if username and password:
        print("current add data is :username:{0},password:{1}".format(username, password))
        add_user = User()
        add_user.username = username
        add_user.password = password
        add_user.save()
        print("do add user option!")
    else:
        print("there hasn't not data to add user table!")
    return redirect(to="../users/")


def to_update_user_page(request):
    model = {}
    if "id" in request.GET and request.GET["id"]:
        id = request.GET["id"]
    if id:
        model["updateUser"] = User.objects.get(id=id)
    else:
        print("there haven't id can't to update user data!")
    return render(request, "updateUser.html", model)


# 通过当前的id编号更新用户
def update_user(request):
    if "id" in request.POST and request.POST['id']:
        id = request.POST["id"]
        update_user = User.objects.get(id=id)
        update_user.username = request.POST["username"]
        update_user.password = request.POST["password"]
        update_user.save()
    else:
        print("there hasn't id cant't to update user data!")
    return redirect(to="../users/")


# 删除数据操作
def del_user_by_id(request):
    if "id" in request.GET and request.GET['id']:
        id = request.GET["id"]
        del_user = User.objects.get(id=id)
        if del_user is not None:
            del_user.delete()
    else:
        print("there hasn't id can't to delete user data!")
    return redirect(to="../users/")


# 用于向当前的数据库中查询所有的数据
def user_list(request):
    print("visit user list data")
    model = dict()
    model["users"] = User.objects.all()
    return render(request, "index.html", model)

发现通过上面的User.objects的方式操作数据库简化了许多的代码!

3.测试

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
以上的所有的操作都执行完成,测试成功!

4.总结

1.当我们再迁移原来的数据操作到现在的models的操作的时候需要重新定义当当前的数据库,由于Django中的db数据默认与我们的不一致所以需要添加:class Meta: db_table = "users"重新指定数据库的名称

2.在当前的操作中需要测试,当前出现了一个错误,就是User.objects.get(id=id)这里需要指定id=需要查询的id编号!

以上纯属个人见解,如有问题请联系本人!

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值