首頁  >  文章  >  後端開發  >  python中關於django對資料庫Mysql的增刪改查操作詳解

python中關於django對資料庫Mysql的增刪改查操作詳解

黄舟
黄舟原創
2018-05-19 10:18:044213瀏覽

下面小編就為大家帶來一篇python django 增刪改查操作 資料庫Mysql。小編覺得蠻不錯的,現在就分享給大家,也給大家做個參考。一起跟著小編過來看看吧

下面介紹一下django增刪改查操作:

##1、view .py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.http import HttpResponse
from polls.models import Test
from django.shortcuts import render
# Create your views here.
# 解决乱码
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
# 数据库操作
def testdb(request):
  test1 = Test(name='温鸿雨2')
  test1.save()
  return HttpResponse("<p>数据添加成功!</p>")

# 查询数据库
def selectDB(request):

  # 通过objects这个模型管理器的all()获得所有数据行,相当于SQL中的SELECT * FROM
  list = Test.objects.all()
  returnvalue = []
  for v in list:
    returnvalue.append(v.name)
    print v.name

  print "++++++++++++获取单个对象++++++++++++++++++"
  # 获取单个对象
  response1 = Test.objects.filter(id=1)
  print response1
  for v1 in response1:
    returnvalue2 = "id : ", v1.id, " 姓名:", v1.name
    print returnvalue2

  print "++++++++++++限制返回的数据 相当于 SQL 中的 OFFSET 0 LIMIT 2;++++++++++++++++++"
  response2 = Test.objects.order_by(&#39;name&#39;)[0:2]
  returnvalue3 = {}
  for v2 in response2:
    returnvalue3[v2.id] = v2.name

  print returnvalue3.items()
  print "+++++++++++输出结果:++++++++++++++++++++++++++++++"
  return HttpResponse(returnvalue3.items())

#修改数据可以使用 save() 或 update():
def updateDB(request):
  # 修改其中一个id=1的name字段,再save,相当于SQL中的UPDATE
  test1 = Test.objects.get(id=1)
  test1.name = &#39;Google&#39;
  test1.save()

  # 另外一种方式 
  #Test.objects.filter(id=1).update(name=&#39;Google&#39;) 
  # 修改所有的列 
  # Test.objects.all().update(name=&#39;Google&#39;)

  return HttpResponse("更新数据成功")

def deleteDB(request):
  # 删除id=1的数据
  test1 = Test.objects.get(id=3)
  test1.delete()
  return HttpResponse("删除数据成功")

2、urls.py

"""pythondjango URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
  https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
  1. Add an import: from my_app import views
  2. Add a URL to urlpatterns: url(r&#39;^$&#39;, views.home, name=&#39;home&#39;)
Class-based views
  1. Add an import: from other_app.views import Home
  2. Add a URL to urlpatterns: url(r&#39;^$&#39;, Home.as_view(), name=&#39;home&#39;)
Including another URLconf
  1. Import the include() function: from django.conf.urls import url, include
  2. Add a URL to urlpatterns: url(r&#39;^blog/&#39;, include(&#39;blog.urls&#39;))
"""
from django.conf.urls import url
from django.contrib import admin
from BlogDjango import views
from polls import views as pollsviews, search, search2

urlpatterns = [
  url(r&#39;^admin/&#39;, admin.site.urls),
  url(r&#39;^hello/+\d&#39;, views.hello),
  url(r&#39;^base/&#39;, views.base),
  url(r&#39;^testdb$&#39;, pollsviews.testdb),
  url(r&#39;^querydb$&#39;, pollsviews.selectDB),
  url(r&#39;^updateDB$&#39;, pollsviews.updateDB),
  url(r&#39;^deleteDB$&#39;, pollsviews.deleteDB),
]

3、models.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models

# Create your models here.

class Test(models.Model):

  name = models.CharField(max_length=20)

以上是python中關於django對資料庫Mysql的增刪改查操作詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn