Django learning little note -ORM Operations Overview django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3

In fact, many web frameworks are used to connect pymysql control database, but Django also provides a database of operational methods ORM (also essentially pymysql); two ways can be, but we need to learn about ORM

  • No longer concerned with that mysql, oracle... and so on.
  • Simple configuration database can easily be replaced without the need to modify the code.

ORM operations:

  http request:

    url -> View (+ template data)

  ORM operating table:

    Creating tables, modifying table, drop table

  Operational data line:

    CRUD

  ORM pymysql use third-party tools such as database connection

  The default sqlite

  Default: mysql -> MYSQLDB (django modify the default mysql connection

 

 

The default setting file such settings:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

We need to modify the following configuration:

DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME':'dbname',
    'USER': 'root',
    'PASSWORD': 'xxx',
    'HOST': '',
    'PORT': '',
    }
}

# 如下设置放置的与project同名的配置的 __init__.py文件中

# The use of the internal module MySQLdb Django MySQL is connected, and also no such python3 module, it is necessary to replace the use of pymysql 

Import pymysql 
pymysql.install_as_MySQLdb ()

If the emergence of this bug fix this ->  django.core.exceptions.ImproperlyConfigured: the mysqlclient 1.3.13 or newer IS required; you have have 0.9.3

Step is to prepare summary:

  1. Create a database
  2. Modify the setting file
  3. Modify the file __init__.py introduced pymysql

 Then you create a class, so we like to generate the table:

In app01 folder of models.py Review:

from django.db Import Models 

# create tables, columns (id \ NID ... 
class UserInfo (models.Model): 
    NID = models.BigAutoField (primary_key = True) # This line does not write Django will automatically create a default id and is self by the primary key, so the future can not write this line! 
    username = models.CharField (max_length = 32 ) 
    password = models.CharField (64-max_length =)

How to make that class to create the table we do? .. again setting are registered on app01 :

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app01'
]
View Code

Finally, we must zoom trick: make-table ! These two commands recorded in the bone marrow! ! !

# If there are no migrations app you must first generate the next file! 
# Python manage.py makemigrations --empty app01 

Python manage.py makemigrations 
Python manage.py the migrate

A look at it all of a sudden addition to our own table there are a lot of tables, in fact, that comes with Django background related to:

 

If you later want to modify the table name in the class, please modify, and then perform a "bone marrow command" on the line! Data does not matter!

If you later want to add an age = models.IntegeField (), add the line directly, but ....

If the original data table by specifying age = models.IntegeField (null = True) or age = models.IntegeField (default = xxx)

To create many relationship and so on, for example, ug = models.ForeignKey ( "UserGroup", null = True) # generate a foreign key ug_id

 

Database-related actions: CRUD

# Test can be deleted 
DEF Test (requst):
     from app01 Import Models
     # new 
    models.UserInfo.objects.create (username = ' Shannon ' , password = ' 111111 ' )
     # find 
    # UserInfo QuerySet that Type (list) [obj, obj , obj] 
    UserInfo = models.UserInfo.objects.all ()
     for Row in UserInfo:
         Print (row.nid, row.username)
     # plus conditioned lookup filter () 
    # , is and; id__gt = 1 id is greater than 1; id__lt = 1 is less than 1 id
    = models.UserInfo.objects.filter UserInfo (NID = 2, username = ' Shannon ' ) 

    # deleted, must first find ~ Truncate 
    models.UserInfo.objects.filter (= NID. 1 ) .Delete () 

    # Update 
    models.UserInfo .objects.filter (= NID. 4) .Update (password = ' 123 ' ) 

    return the HttpResponse ( ' success ' )

 

Implement

Guess you like

Origin www.cnblogs.com/ethtool/p/12171368.html