Learning Framework django: 12 data operation CRUD

Foreword

django of modles module where you can add a table and field operations are usually pages from additions and deletions to change search the database, how to achieve the additions and deletions to change search the database by django it?

New database data

Previous We created ke24_user table, use_name = "liushui", psw = "123456", mail = "[email protected]"

Creating testdb.py file in the same directory urls, create add_user as follows:

 

 The method created above to create an access path urls.py.

 

 Open the terminal command line:

python manage.py runserver 0.0.0.0:8000

Open a browser: ip: 8000 / add_user / display a user to create a successful visit

Open navicat client, see the table new data.

 

 update data

If you want to update the data in the database, such as trying to change the database user psw liushui is "654321", in testdb.py file, create update method.

python manage.py runserver 0.0.0.0:8000

Open a browser: ip: 8000 / update_db / display a user to create a successful visit

Open navicat client, see the table new data.

delete data

If the user table inside a user does not want the data, you can delete data

 

 

Urls.py add new access path

 

 

 

 

python manage.py runserver 0.0.0.0:8000

Open a browser: ip: 8000 / delete_user / display a user to create a successful visit

Open navicat client data deleted successfully.

Query the database

For example, I would like to check mail corresponding values ​​in the database liushui

In testdb.py script urls.py the same directory, continue to write a function select_psw

urls.py url to access the new address

 

 

python manage.py runserver 0.0.0.0:8000

Open a browser: ip: 8000 / select_mail / display a user to create a successful visit

Open navicat client, the query was successful.

Relevant Summary:

All data rows obtained by the model manager objects all (), corresponding to the FROM SQL * in the SELECT
A = User.objects.all () 

filter is equivalent to the SQL WHERE, filter the results set condition
b = User.objects.filter (id = 1)

Gets a single object

c = User.objects.get(id=1)

Limit returned data corresponds to the SQL OFFSET 0 LIMIT 2;

d = User.objects.order_by('name')[0:2]

Sorting Results

e = User.objects.order_by("id")

The above method can be attached using

f = User.objects.filter(name="runoob").order_by("id")

Guess you like

Origin www.cnblogs.com/liushui0306/p/12614653.html