Add, delete, change, and check the data records in Django

Source: Internet
Author: User

Data additions and deletions in Django are as follows:

We take a model:user for example, user has three fields, one is username, passwd, PhoneNumber

(1) Add a record

Add a record for the username, passwd, phonenumber field values S_username, S_PASSWD, S_phonenumber

User=user ()

User.username=s_username

user.passwd=s_passwd

User.phonenumber=s_phonenumber

User.save ()

Or

User=user (Username=s_username,passwd=s_passwd,phonenumber=s_phonenumber)

User.save ()

(2) Deleting records

Delete a record of username as ' yy '

User.objects.get (username= ' yy '). Delete ()

Delete a set of records, assuming that a phone number can correspond to more than one user, now remove all users with a phone number of ' 123456 '

User.objects.filter (phonenumber= ' 123456 '). Delete ()

(3) Change of record

Change the phone number of the record username as ' DW ' to ' 88888 '

User=user.objects.get (username= ' DW ')

User.phonenumber= ' 88888 '

User.save ()

(4) Inquiry record

Querying for records with username as ' DW '

User=user.objects.get (username= ' DW '), the record with Get query is a User object

Query PhoneNumber for all users with ' 88888 '

User=user.objects.filter (phonenumber= ' 88888 ') with the result of the filter query is a list






Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Add, delete, change, and check the data records in Django

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.