一、orm建表

  1. 先在modles.py中通过类及其属性确定表结构,然后通过迁移创建表格

  2. 迁移生成表格

    python3 manage.py makemigrations
    python3 manage.py migrate
    

该方式会在数据库中创建多份关于django的表

Django(三十二)—— Django中建表方式

 

二、先建表在写类

  1. 手动在数据库中建表,确定好表名、字段名、字段类型

  2. 根据确定的表名、字段名、字段类型等,在modles.py中写好对应的类,并通过Meta指定表名

    class Stu(models.Model):
        name = models.CharField(max_length=100)
        
        class Meta:
            db_table = 'stu'
    

该方式不需要迁移,即可通过orm直接使用,且在数据库中不会出现多余的表

相关文章:

  • 2021-12-09
  • 2022-01-29
  • 2022-01-02
  • 2021-09-07
  • 2022-01-01
  • 2021-08-20
  • 2022-12-23
  • 2021-04-09
猜你喜欢
  • 2021-10-24
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-19
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案