Model (operation table) and djangomodel operation table in Django

Source: Internet
Author: User

Model (operation table) and djangomodel operation table in Django
Model operation table

 

I. Basic operations
1 # Add 2 3 models. tb1.objects. create (c1 = 'XX', c2 = 'oo ') # Add a data record and accept Dictionary data ** kwargs 4 5 obj = models. tb1 (c1 = 'XX', c2 = 'oo ') 6 obj. save () 7
Dic = {'c1 ': 'XX', 'c2': 'oo '}
Models. Tb1.objects. create (** dic) # The output result of Form is a dictionary. You can create data directly in the database based on the dictionary of this Form and **.
8 # query 9 10 models. tb1.objects. get (id = 123) # get a single piece of data. If no data exists, an error is returned (not recommended) 11 models. tb1.objects. all () # Get all. first () Fetch the first data 12 models. tb1.objects. filter (name = 'seven') # You can use the ** method to obtain the data of a specified condition to pass the parameter 13 14 # Delete 15 16 models. tb1.objects. filter (name = 'seven '). delete () # delete the data of the specified condition 17 18 # Change 19 20 models. tb1.objects. filter (name = 'seven '). update (gender = '0') # update the data of the specified condition. ** kwargs21 22 obj = models is supported. tb1.objects. get (id = 1) 23 obj. c1 = '000000' 24 obj. save () # modify a single data record

 

Take a closer look at the data types retrieved from the database:

1 w = models.Simp.objects.all()2 print w, type(w)
3 [<Simp: chenc>, <Simp: zan>, <Simp: zhangsan>] <class 'django.db.models.query.QuerySet'>

 

 As you can see, retrieving data from the database looks like a list of contained objects. In fact, the whole data is a special type in django.QuerySet.

. All ()Is to obtain data of all columns, you can add. Values ()Extracts a column, and the value of each item isDictionary:

1 w = models.Simp.objects.all().values('username')2 print w, type(w)

[{'username': u'chenc'}, {'username': u'zan'}, {'username': u'zhangsan'}] <class 'django.db.models.query.QuerySet'>

. Values_list (),The obtained value isTuples

1 w = models.Simp.objects.all().values_list('username')2 print w, type(w)

[(u'chenc',), (u'zan',), (u'zhangsan',)] <class 'django.db.models.query.QuerySet'>

. Values_list ()You can also add multiple parameters: (you can use Form to generate a dynamic select statement on the front-end)

1 w = models.Simp.objects.all().values_list('id', 'username')2 print w, type(w)

[(1, u'chenc'), (2, u'zan'), (3, u'zhangsan')] <class 'django.db.models.query.QuerySet'>

 

QueryYou can view the executed SQL statement:

1 b = models.Simp.objects.all()2 print b.query

SELECT "app01_simp"."id", "app01_simp"."username", "app01_simp"."password" FROM "app01_simp"

 

 

II,

 

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.