MTV與MVC
MTV模型(django):
M:模型層(models.py)
T:templates
V:views
MVC模型:
M:模型層(models.py)
V:視圖層(views.py)
C:控制器(Controller) urls.py
本質:django的MTV也是MVC
多對多表三種建立方式
1.第一種 django orm自動幫咱們建立
class Book(models.Model):
name = models.CharField(max_length=32)
authors = models.ManyToManyField(to='Author')
class Author(models.Model):
name = models.CharField(max_length=32)
2.第二種純手動建立第三張表
class Book(models.Model):
name = models.CharField(max_length=32)
class Author(models.Model):
name = models.CharField(max_length=32)
class Book2Author(models.Model):
book = models.ForeignKey(to='Book')
author = models.ForeignKey(to='Author')
info = models.CharField(max_length=32)
3.第三種半自動建立第三張表(可擴展性高,而且可以符合orm查詢)
class Book(models.Model):
name = models.CharField(max_length=32)
# 第三種建立表的方式
#想用django的一些方法,因而告訴django這兩張表有多對多關係
authors = models.ManyToManyField(to='Author',through='Book2Author',through_fields=('book','author'))
class Author(models.Model):
name = models.CharField(max_length=32)
# book = models.ManyToManyField(to='Book',through='Book2Author',through_fields=('author','book'))
class Book2Author(models.Model):
book = models.ForeignKey(to='Book')
author = models.ForeignKey(to='Author')
info = models.CharField(max_length=32)
# 第三種多對多關係不能使用多對多字段的增刪改查方法 add(),set(),remove(),clear()