Model: Powerful database operation, weak data validation
Form: Powerful data validation
Modelform: Powerful data validation + weak database operation
Model
SupplementsModel Basic Operation
1. Create a database table
2. Modifying table-level and row-level data
2.1 Data Sheet operation
1. Code First: Create Class--auto-Generate table "Django"
2. Database First: CREATE TABLE--auto-Generate class "Hibernate&mybatit"
Single table:
One:
One-to-many: only ForeignKey, constrained relationships
Note: A one-to-many party creates foreignkey[from a SQL perspective]
Many-to-many: 1. Django helped us create the third table
Models. Manytomanyfield---no fields are added to the class, just help us create the third table
2. We create our own third sheet "recommended"
1. Create a class that defines 2 foreign keys, at which point the table relationship is at a glance
3. We create our own third table and let Django refer to it
Note: Many-to-many only involves a positive and negative lookup problem, written in any class can be
When you create a third table with Manytomanyfield, the contents of the field are added to the djangoadmin.
The custom third table needs to be added favor class to admin to display
2.2 Data row operations
use Django to help us create a third table effect:
settings.py
Installed_apps = [ ... ' App01 ', # Register App]staticfiles_dirs = (Os.path.join (base_dir, "statics"), # Now add the configuration, here is the tuple, note the comma templates = [ ... ' DIRS ': [Os.path.join (Base_dir, ' templates ')],]
urls.py
From Django.contrib import adminfrom django.urls import pathfrom django.conf.urls import URL, includefrom app01 import vie Wsurlpatterns = [ ]
views.py
From django.shortcuts import Render, redirect, Httpresponsefrom APP01 import Models
models.py
From django.db import Modelsclass User (models. Model): id = models. Autofield (primary_key=true) # Autofield must be a primary key in order to customize the column name = models. Charfield (max_length=32) usertype = models. ForeignKey ("Usertype", On_delete=true) # 1-to-many [cannot be customized, constrained] # Many-to-many [1. Create your own third table 2. Manytomany] # in the djangoadmin if from You have created a third table, there will be no dropdown box in Djangoadmin # manytomany after creating the constraint, the djangoadmin will have a drop-down box [because Djangoadmin uses modelform, it will show this as a field]class Usertype (models. Model): name = models. Charfield (MAX_LENGTH=32) class News (models. Model): title = models. Charfield (max_length=32) # Django helped us create the third table favor favor = models. Manytomanyfield ("User") # Djangoadmin shows 2 fields [title + Favor Multi box]# custom third table, which refers to many2many create third table "Admin not registered in this class" class Favor (models. Model): new = models. ForeignKey ("News", On_delete=true, related_name= ' u ') user = models. ForeignKey ("User", On_delete=true, Related_name= ' n ')
app01/admin.py
From Django.contrib import admin# Register your models here.from APP01 import modelsadmin.site.register (models. User) Admin.site.register (models. News) Admin.site.register (models. usertype)
page display;
Initializing the database and Admin user
Python manage.py makemigrationspython manage.py migratepython manage.py createsuperuser
Use the favor effect of the custom third table:
models.py
From django.db import Modelsclass User (models. Model): id = models. Autofield (primary_key=true) # Autofield must be a primary key in order to customize the column name = models. Charfield (max_length=32) usertype = models. ForeignKey ("Usertype", On_delete=true) # 1-to-many [cannot be customized, constrained] # Many-to-many [1. Create your own third table 2. Manytomany] # in the djangoadmin if from You have created a third table, there will be no dropdown box in Djangoadmin # manytomany after creating the constraint, the djangoadmin will have a drop-down box [because Djangoadmin uses modelform, it will show this as a field]class Usertype (models. Model): name = models. Charfield (MAX_LENGTH=32) class News (models. Model): title = models. Charfield (max_length=32) # Django helped us create the third table favor "Here does not reference it" # favor = models. Manytomanyfield ("User") # Customizes the third table, "register the class in Admin, refer to Custom Table" class Favor (models. Model): new = models. ForeignKey ("News", On_delete=true, related_name= ' u ') user = models. ForeignKey ("User", On_delete=true, Related_name= ' n ')
app01/admin.py
From Django.contrib import admin# Register your models here.from APP01 import modelsadmin.site.register (models. User) Admin.site.register (models. News) Admin.site.register (models. usertype) Admin.site.register (models. Favor)
Page display:
Use the Many2many + custom third table:
The form is changed and needs to be re-written to the database
Attention:
1. Because the Through_field () we use is associated with some of the favor fields, it is not associated with all, so the page cannot add data directly.
2. After we use through to correlate custom favor and news, it is not possible to use the Add (), remove () operation method directly,
However, you can use all (), filter () to find the operation. At the same time Obj.favor.clear () can also be used ...
models.py
From django.db import Modelsclass User (models. Model): id = models. Autofield (primary_key=true) # Autofield must be a primary key in order to customize the column name = models. Charfield (max_length=32) usertype = models. ForeignKey ("Usertype", On_delete=true) # 1-to-many [cannot be customized, constrained] # Many-to-many [1. Create your own third table 2. Manytomany] # in the djangoadmin if from You have created a third table, there will be no dropdown box in Djangoadmin # manytomany after creating the constraint, the djangoadmin will have a drop-down box [because Djangoadmin uses modelform, it will show this as a field]class Usertype (models. Model): name = models. Charfield (MAX_LENGTH=32) class News (models. Model): title = models. Charfield (max_length=32) # Django helped us create the third table and point to our custom favor table and add the specified field favor = models. Manytomanyfield ("User", through= ' Favor ', through_fields= (' new_obj ', ' User_obj ')) # Custom Third table, "register this class in Admin, refer to Custom table" Class Favor (models. Model): New1 = models. ForeignKey ("News", On_delete=true, Related_name= ' U1 ') # does not reference the field new_obj = models. ForeignKey ("News", On_delete=true, related_name= ' u ') user_obj = models. ForeignKey ("User", On_delete=true, Related_name= ' n ')
app01/admin.py
From Django.contrib import admin# Register your models here.from APP01 import modelsadmin.site.register (models. User) Admin.site.register (models. News) Admin.site.register (models. usertype) Admin.site.register (models. Favor)
Page display:
Initializing the database
Python manage.py Makemigrationspython manage.py Migrate
Problem solving:
Problem phenomenon:
Problem Locator:
Django helped create many-to-many news.favor and our custom class Favor, at this time 2 is a function, so the system prompts us to query the name conflict, add a query alias can be resolved.
One-to-one operation: Association of tables and tables
2 ways:
UserProfile = models. ForeignKey ("UserProfile", On_delete=true, Unique=true) # Unique index
Userdetail = models. Onetoonefield ("UserProfile", On_delete=true) # Another way of writing
Note: If we encounter a form with many columns, you can reduce the time of the SQL query by splitting the key stand-alone form and then associating it with a one-to-another relationship between the table and table
models.py
From django.db import Modelsclass User (models. Model): id = models. Autofield (primary_key=true) # Autofield must be a primary key in order to customize the column name = models. Charfield (max_length=32) usertype = models. ForeignKey ("Usertype", On_delete=true) # 1 To many [cannot be customized, constrained] userprofile = models. ForeignKey ("UserProfile", On_delete=true, Unique=true) # One-to-one, unique index userdetail = models. Onetoonefield ("UserProfile", On_delete=true) # Another way of writing # SQL Optimization: fixed-length fields are placed at the front of class userprofile (models. Model): pwd = models. Charfield (MAX_LENGTH=32)
For more information:
Model operation;:http://www.cnblogs.com/wupeiqi/articles/6216618.html
form operation;:http://www.cnblogs.com/wupeiqi/articles/6144178. html
Model operation;:http://www.cnblogs.com/wupeiqi/articles/6229414. html
Python Learning---model supplements [1]180318