Examples of add, delete, modify and query operations in Django database

巴扎黑
Release: 2017-09-05 11:14:53
Original
2377 people have browsed it

The following editor will bring you an example of Django database operation (add, delete, modify, check). The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.

Create a table in the database


class Business(models.Model):
 #自动创建ID列
 caption = models.CharField(max_length=32)
 code = models.CharField(max_length=32)
Copy after login

1. Add

Method one


models.Business.objects.create(caption='市场部',code='123')
Copy after login

Method two


obj = models.UserInfo(caption='市场部',code='123')
obj.save()
Copy after login

Method Three


dic = {'caption':'市场部','code':'123'}
models.Business.objects.create(**dic)
Copy after login

2. Delete


models.Business.objects.filter(id=1).delete()
Copy after login

For the query method, see query below

##3. Change

Method one


models.Business.objects.filter(id=1).update(code='hello')
Copy after login

Method two


obj = models.Business.objects.get(id=1)
obj.code = 'hello'
obj.save()
Copy after login

Query See the method below for query

4. Query

get all

##

v1 = models.Business.objects.all()  #QuerySet类型,内部元素都是对象
Copy after login

get Specify

v2 = models.Business.objects.all().values("id","caption") #QuerSet类型,内部元素都是字典
v3 = models.Business.objects.all().values_list('id','caption') #QuerySet类型,内部元素都是元组
v4 = models.Business.objects.get(id=1) #获取一个队象,如果不存在就报错
v5 = models.Business.objects.filter(id=1) #QuerySet类型,内部元素是对象,id__gt=1获取所有id>1的数据,id__lt=10,获取所有id<10的数据
v6 = models.Business.objects.filter(id=1).first() #返回对象或者None
Copy after login

Application instance

business function

def business(request):
 v1 = models.Business.objects.all()
 v2 = models.Business.objects.all().values("id","caption")
 v3 = models.Business.objects.all().values_list(&#39;id&#39;,&#39;caption&#39;)
 return render(request,"business.html",{"v1":v1,"v2":v2,"v3":v3})
Copy after login

 url(r&#39;^business$&#39;,views.business)
Copy after login

business.html

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Document</title>
</head>
<body>
 <ul>
  <h1>ALL</h1>
  {% for row in v1 %}
   <li>{{row.id}}-{{row.caption}}-{{row.code}}</li>
  {% endfor %}
 </ul>
 <ul>
  <h1>all.values</h1>
  {% for row in v2 %}
   <li>{{row.id}}-{{row.caption}}</li>
  {% endfor %}
 </ul>
 <ul>
  <h1>all.values_list</h1>
  {% for row in v3 %}
   <li>{{row.0}}-{{row.1}}</li>
  {% endfor %}
 </ul> 
</body>
</html>
Copy after login

The above is the detailed content of Examples of add, delete, modify and query operations in Django database. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template