Ways to update database data in Python's Django framework

Source: Internet
Author: User
Create an object instance with some key parameters, as follows:

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

The object instance did not modify the database. The record is not saved to the database until the ' Save () ' method is called, like this:

>>> P.save ()

In SQL, this can be roughly converted to this:

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 auto-incremented primary key ID, the first call to save () does one more thing: Calculate the value of the primary key and assign it to the object instance:

>>> P.id52  # This would differ based on your own data

The next call to save () will not create a new record, but simply modify the contents of the record (that is, execute the UPDATE SQL statement instead of the INSERT statement):

>>> p.name = ' Apress publishing ' >>> P.save ()

The previously executed save () corresponds 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 the modified field is updated, but all the fields are updated. This operation has the potential to cause a race condition, depending on your application. See the section "Updating multiple Objects" later to learn how to implement this lightweight modification (only part of the object's fields are modified).

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

Select Object

Of course, creating a new database and updating the data in it is necessary, but for Web applications, more often than not, the query database is retrieved. We already know how to remove all the records from a given model:

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

This is equivalent to this SQL statement:

SELECT ID, name, address, city, State_province, country, Websitefrom books_publisher;

Attention

Note that Django does not use select* when selecting all the data, but instead explicitly lists all the fields. This is how it is designed: select* will be slower, and most importantly, listing all the fields follows a tenet of the Python community: speak louder than hints.

For Python's Zen (commandment):-), enter import this in the Python hint line to try it out.

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

    • First, we have a defined model for Publisher. There's nothing strange about it: you want to find data, you use models to get the data.
    • Then, it is the objects property. It is called the manager and we will discuss it in detail in the 10th chapter. For now, we just need to understand that the manager manages all table-level operations for data inclusions and most important data queries.
    • All models automatically have a objects manager, and you can use it when you want to find 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 some records in the database.
  • Related Article

    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.