zoukankan      html  css  js  c++  java
  • 多对多第三张表的三种创建方式

    • orm自动创建(可扩展性差,无法即时添加其他字段)
    class Book(models.Model):
    name = models.CharFiled(max_length=32)
    authors = models.ManyToManyField(to="Author")
    
    class Author(models.Model):
    name = models.CharField(max_length=32)
    • 手动创建第三张表(不支持直接跨表查询)
    class Book(models.Model):
    name = models.CharField(max_length=32)
    
    class Author(models.Model):
    name = models.CharFiled(max_length=32)
    
    class Book2Author(models.Model):
    book = models.ForeignKey(to="Book")
    Author = models.ForeignKey(to="Author")
    info = models.CharFiled(max_length=32)


    手动创建第三张表,并通过字段进行关联(可扩展性搞,支持直接跨表查询)

    class Book(models.Model):
    name = models.CharFiled(max_length=32)
    authors = models.ManyToManyField(to="Author", through="Book2Author", through_fields=("book", "author"))
    
    
    class Author(models.Model):
    name = models.CharFiled(max_length=32)
    #books = models.Many2ManyField(to="Book", through="Book2Author", through_fields=("author", "book"))  # 与book表中的最后一个字段二选一
    
    class Book2Author(models.Model):
    book = models.ForeignKey(to="Book")
    Author = models.ForeignKey(to="Author")
    info = models.CharField(max_length=32)
    

      

  • 相关阅读:
    sql server mdx
    mysql 按照 汉字的第一个拼音排序
    转,mysql的select * into
    mysql 日期的操作
    google 地图api
    ip_test
    AJAX (转w3cschool)
    jquery ajax 失败
    安装AdventureWorks2008R2示例数据库
    弹出新的网页窗口 js
  • 原文地址:https://www.cnblogs.com/penghengshan/p/11025210.html
Copyright © 2011-2022 走看看