model Form ModelForm

model

  1. 创建数据库表

     # 单表
     # app01_user ==> tb1
     # users
     class User(models.Model):
     	name = models.CharField(max_length=32)
     	pwd = models.CharField(max_length=32)
     	
     	class Meta:
     		# 数据库中生成的表名称 默认:app名称 + 下划线 + 类名
     		db_table = "tb1"
    
     		# 联合索引
     		index_together = [
     			("name", 'pwd'),
     		]
     		# 最左前缀的模式
     		# select * from where name='xx'
     		# select * from where name='xx' and email = 'xx'
     		# select * from where email = 'xx' 		# 无法命中索引
     		
     		unique_together = (("driver", "restaurant"),)
     		
     		verbose_name = “上课记录”
     		verbose_name_plural=““上课记录””
     # 一对一
     	思想:ForeignKey + 唯一索引
    
     	models.OneToOneField('Host')
     # 一对多
    
     	
     # 多对多
     	思想:双向ForeignKey
    
     	models.ManyToManyField("Host")
    
  2. 操作数据库表

  3. 数据验证(弱)

Form

  1. 数据验证(强大)

ModelForm

  1. 数据库操作

  2. 数据验证

    小型 自定制admin

注:强耦合,不建议使用

ModelForm验证 -> Form验证 -> model

原文地址:https://www.cnblogs.com/todayisafineday/p/8783741.html