The Python class name---------------the table name of the >sql statement Python class Property---------------The table name of the >SQL statement constraints on the class properties of Python---------------> An instance object of the field type class for the table name of the SQL statement ---------------a record in the > table
first, create a table (build model )
1. Create a class (create table)
Create the class in the app's models.py file, as in the following example:
From django.db import Modelsclass Student (models. Model): #必须继承models. Model class Sid=models. Autofield (primary_key=true) #主键且自增 name=models. Charfield (max_length=11) #字符串类型 and the maximum number of characters is birth=models. Datefield () #日期类型, such as: y-m-d
Then, after terminal executes the following two commands, the models.py file is automatically executed to generate the corresponding table for the defined class:
Python manage.py Makemigrationspython manage.py Migrate
If we need to add a field to a table that has already been built, we just need to add the field to the corresponding class and then re-execute the above two commands, but it is important to emphasize that the added field must be set to a default value, otherwise the command will be executed successfully. The default value is set to ensure that new fields of data rows that were previously inserted have values. The following example:
From django.db import Modelsclass Student (models. Model): sid=models. Autofield (primary_key=true) name=models. Charfield (max_length=11) birth=models. Datefield () age=models. Integerfield (default=3) #整数类型, you need to set a default value for the new field later
Second, add data
Models file is only responsible for defining the class (CREATE table), table data additions and deletions are executed in the Views view function, add data instance as follows:
From app01.models import studentdef Add (Request): if request.method== "POST": stu_name=request. Post.get ("name") stu_birth=request. Post.get ("Birth") stu_age=request. Post.get ("Age") #方式一: s=student (name=stu_name,birth=stu_birth,age=stu_age) s.save () #保存数据到数据库 #方式二: Student.objects.create (name=stu_name,birth=stu_birth,age=stu_age) return Redirect ("/index/") return Render (Request, "add.html")
The above example needs to be noted: Although the models.py file and the views.py file are in the same application directory, they cannot be written from the models import Student in the reference module above. Because the file that executes our project is the manage.py file, not the same directory as the file under Application App01, but the same class as the App01 file, so the corresponding module is not found at all.
Table data additions and deletions to the operation, in fact, will be translated into the corresponding SQL statement, in the setting file, the following settings, you can perform data operations on the screen to print the corresponding SQL statement:
LOGGING = { ' version ': 1, ' disable_existing_loggers ': False, ' handlers ': { ' console ': { ' level ': ' DEBUG ', ' class ': ' Logging. Streamhandler ', }, }, ' loggers ': {' django.db.backends ': {' handlers ': [' console '], ' Propagate ': True, ' level ': ' DEBUG ', },} }
The result of the screen printing after executing the Add function:
Third, edit the data
First, follow the ORM method, query to meet the requirements of the Queryset object, and then update () will meet the requirements of the object to be updated, the example is as follows:
From app01.models import studentdef edit (request,id): if request.method== "POST": stu_name=request. Post.get ("name") stu_birth=request. Post.get ("Birth") stu_age=request. Post.get ("Age") Student.objects.filter (tid=id). Update (Name=stu_name,birth=stu_birth,age=stu_age) Return Redirect ("/index/") stu=student.objects.filter (Tid=id) [0] return render (Request, "edit.html", {"Stu ": Stu})
iv. deletion of data
First, according to the ORM method, query to meet the requirements of the Queryset object, and then delete () will meet the requirements of the object to delete, the example is as follows:
def delet (request,id): Student.objects.filter (tid=id). Delete () return redirect ("/index/")
Python Day73django ORM Model