Instances operated by the Django database (add, delete, modify, and query), and add or delete django

Source: Internet
Author: User

Instances operated by the Django database (add, delete, modify, and query), and add or delete django

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

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

Method 2

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

Method 3

Dic = {'caption ': 'marketing Department', 'code': '000000'} 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

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

Method 2

obj = models.Business.objects.get(id=1)obj.code = 'hello'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 all tuples v4 = models. business. objects. get (id = 1) # get a team image. If it does not exist, the system reports the v5 = models error. 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 of all IDs <10 v6 = models. business. objects. filter (id = 1 ). first () # Return object or None

Application Instance

Business functions

def business(request): v1 = models.Business.objects.all() v2 = models.Business.objects.all().values("id","caption") v3 = models.Business.objects.all().values_list('id','caption') return render(request,"business.html",{"v1":v1,"v2":v2,"v3":v3})
 url(r'^business$',views.business)

Business.html

<!DOCTYPE html>

The above Django database operation instance (add, delete, modify, and query) is all the content shared by Alibaba Cloud xiaobian. I hope you can give us a reference and support for our customer base.

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.