How to update database data in the Python Django framework

Source: Internet
Author: User
This article mainly introduces how to update database data in the Python Django framework, which provides convenient insertion and update methods, you can refer to the following key parameters to create an object instance:

>>> p = Publisher(name='Apress',...     address='2855 Telegraph Ave.',...     city='Berkeley',...     state_province='CA',...     country='U.S.A.',...     website='http://www.apress.com/')

This object instance has not modified the database. Before calling the ''save () ''method, the record is not saved to the database, as shown in the following code:

>>> p.save()

In SQL, this can be roughly converted to the following:

INSERT INTO books_publisher  (name, address, city, state_province, country, website)VALUES  ('Apress', '2855 Telegraph Ave.', 'Berkeley', 'CA',   'U.S.A.', 'http://www.apress.com/');

Because the Publisher model has an automatically added primary key id, the first time you call save (), you do one more thing: calculate the value of this primary key and assign it to this object instance:

>>> p.id52  # this will differ based on your own data

Next, call save () to create a new record, instead of modifying the Record Content (that is, executing the update SQL statement instead of the INSERT statement ):

>>> p.name = 'Apress Publishing'>>> p.save()

The save () Statement executed earlier is equivalent to the following SQL statement:

UPDATE books_publisher SET  name = 'Apress Publishing',  address = '2855 Telegraph Ave.',  city = 'Berkeley',  state_province = 'CA',  country = 'U.S.A.',  website = 'http://www.apress.com'WHERE id = 52;

Note that not only modified fields are updated, but all fields are updated. This operation may cause race conditions, depending on your application. See the "Update multiple objects" section to learn how to implement this lightweight modification (only modifying some fields of the object ).

UPDATE books_publisher SET  name = 'Apress Publishing'WHERE id=52;

Select object

Of course, it is necessary to create a new database and update the data. However, for Web applications, it is more often necessary to search and query the database. We already know how to retrieve all records from a given model:

>>> Publisher.objects.all()[
 
  , 
  
   ]
  
 

This is equivalent to the SQL statement:

SELECT id, name, address, city, state_province, country, websiteFROM books_publisher;

Note:

Note that Django does not use SELECT * When selecting all data, but explicitly lists all fields. The design process is like this: SELECT * will be slower, and the most important thing is to list all the fields following one of the principles of the Python field: clearly better than implied.

For Python ZEN (commandment) :-), enter import this in the Python prompt line.

Let's take a closer look at each part of the Publisher. objects. all () line:

  • First, we have a defined model Publisher. Nothing strange: If you want to search for data, you can use the model to obtain data.
  • Then, it is the objects attribute. It is called a manager and will be discussed in detail in Chapter 10th. At present, we only need to know that the Manager manages all table-level operations for data inclusion and the most important data queries.
  • All models automatically have an objects manager, which you can use to search for data.
  • Finally, there is the all () method. This method returns all records in the database. Although this object looks like a list, it is actually a QuerySet object, which is a collection of records in the database.

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.