zoukankan      html  css  js  c++  java
  • Django多对多表的三种创建方式,MTV与MVC概念

        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()
  • 相关阅读:
    Common Words
    The end of other
    避免ssh断开导致运行命令的终止:screen
    Check iO:初学Python
    linux环境c++开发:ubuntu12.04使用llvm3.4.2
    boost库使用:仿SGI-STL实现的一个树节点allocator
    boost库使用:vs2013下boost::container::vector编译出错解决
    读论文系列:Nearest Keyword Search in XML Documents中使用的数据结构(CT、ECT)
    房产律师咨询
    E文阅读
  • 原文地址:https://www.cnblogs.com/dongxixi/p/11042883.html
Copyright © 2011-2022 走看看