Django database CRUD

First, the database configuration:

django default using built-in sqlite database

If you want to change other databases, you need to configure the settings configuration file

. 1 .settings configuration file 
                DATABASES = {
             ' default ' : {
                 ' ENGINE ' : ' django.db.backends.mysql ' ,
                 ' NAME ' : ' day51 ' ,
                 ' the HOST ' : ' 127.0.0.1 ' ,
                 ' PORT ' : 3306 ,
                 ' the USER ' : ' the root ',
                 ' PASSWORD ' : ' 123 ' ,
                 ' CHARSET ' : ' utf8 ' 
            } 
}
 2 . Django also told not to use the default connection mysqldb mysql init files in the project name or the name of the application init file 
        instead use pymysql 
            Import pymysql 
            pymysql.install_as_MySQLdb ()

 

Second, create a model class

class User (models.Model):
      # the id field to the User table primary key fields in django orm you can not write to the master key django dictionary will default to your table to create a primary key field named id 
      # id = models.AutoField ( primary_key = True) # Once you specify the primary key field that will not be django will help you automatically create a 
      username = models.CharField (max_length = 32)   # username VARCHAR (32) CharField max_length parameters must be specified 
      password = models.IntegerField ()   # password int



************************* need to perform database migration (synchronous) command ************ ****************** python3 manage.py makemigrations # only in (migrations folder) to modify the database records of the little book does not directly manipulate the data python3 manage.py migrate # will modify the database record actually synchronized to the database
  Note: just move the models in the code associated with the database must be re-implementation of the above two commands are indispensable (******)

 

Third, Field CRUD

  When the table has been created, the follow-up to add a new field, there are two ways:

    The new field is set to 1. Default:

models.CharField = addr (= 32 MAX_LENGTH, default = ' China ' )   # default value of this field defaults

    2. Give the new field to be empty

models.IntegerField = Age (null = True)   # This field allows null

  delete

  Note models.py directly in the field, and then re-execute the command to two

 

Fourth, data CRUD

 

Check data
         from app01 Import Models 
        
        models.User.objects.all ()   # direct to get all the data 
        
        models.User.objects.get (username = username)   
        
        RES = models.User.objects.filter (username = username) 
        RES. Query 
        
        USER_OBJ = res.first () 
        
    by data
         . 1 . 
        models.User.objects.create (username = username, password = password)
         2 . 
        USER_OBJ = models.User (username = username, password = password) 
        user_obj.save () 
    
    delete
        models.User.objects.filter(条件).delete()
    
    改
        models.User.objects.filter(条件).update()

 

Fifth, the user CRUD

CRUD user
         1 by showing all the front end orm 
            all () 
            template syntax for loop
         2 . Add New button (the user's new operation) 
            the href A rear end of the label directly trigger logic 
            Create ()
         . 3 . Add Edit delete button 
            edit 
            delete 
                using the get request carrying an id value of the characteristic parameter corresponding to the data to keep the back of the url 
                request.GET.get () 
                
                is an editing 
                    re-rendering a page to the front end of the edit object passes 
                
                if deleted 
                    directly filter (. ..). delete ()

 

Guess you like

Origin www.cnblogs.com/xiaowangba9494/p/11530542.html