Three ways to create ways Django-many table

The first:

  django orm automatically help us to create:

  Table I:

class Book(models.Model):
    name = models.CharField(max_length=32)
    authors = models.ManyToManyField(to='Author')

  Table II:

class Author(models.Model):
    name = models.CharField(max_length=32)

  This way you can make Django quickly to help us build a table out, the benefits can be cross-table queries through this table, the downside is that a virtual table, expand poor.

The second:

  Deposit manually create the third table:

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)
    

  This way can not be cross-table queries by orm (do not use)

Third:

  Semiautomatic create the third table (high scalability, and able to meet orm query)

class Book(models.Model):
    name = models.CharField(max_length=32)
    in the authors = models.ManyToManyField (to = ' Author ' , through = ' Book2Author ' , through_fields = ( ' Book ' , ' author ' )) # on the priority write what tables are tables which create lowercase
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)

 

Guess you like

Origin www.cnblogs.com/ay742936292/p/11023716.html