Django database operations (add, delete, modify, and query) and django addition and Deletion

Source: Internet
Author: User

Django database operations (add, delete, modify, and query) and django addition and Deletion
Django database operations (add, delete, modify, and query)

Create a table in the database

Class Business (models. Model): # automatically create the ID column caption = models. CharField (max_length = 32) code = models. CharField (max_length = 32)

 

1. Add

Method 1

1 models. Business. objects. create (caption = 'marketing Department ', code = '123 ')

Method 2

1 obj = models. UserInfo (caption = 'marketing Department ', code = '000000') 2 obj. save ()

Method 3

1 dic = {'caption ': 'marketing Department', 'code': '000000'} 2 models. Business. objects. create (** dic)

 

2. Delete
models.Business.objects.filter(id=1).delete()
For the query method, see the query below

 

3. Change

Method 1

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

Method 2

1 obj = models.Business.objects.get(id=1)2 obj.code = 'hello'3 obj.save()

For the query method, see the query below

 

4. Query

Get all

V1 = models. Business. objects. all () # QuerySet type, internal elements are all objects

Get specified

V2 = models. business. objects. all (). values ("id", "caption") # QuerSet type, internal elements are dictionaries v3 = models. business. objects. all (). values_list ('id', 'caption ') # The QuerySet type. The internal elements are tuples.
V4 = models. Business. objects. get (id = 1) # obtain a team image. If it does not exist, an error is returned.
V5 = models. business. objects. filter (id = 1) # QuerySet type, the internal element is an object, id _ gt = 1 get all id> 1 data, id _ lt = 10, obtain data with all IDs <10
V6 = models. Business. objects. filter (id = 1). first () # Return object or None

Application Instance

1 def business(request):2     v1 = models.Business.objects.all()3     v2 = models.Business.objects.all().values("id","caption")4     v3 = models.Business.objects.all().values_list('id','caption')5     return render(request,"business.html",{"v1":v1,"v2":v2,"v3":v3})
Business functions
1 url(r'^business$',views.business)
URL
 1 <!DOCTYPE html> 2 Business.html

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.