Django自动生成table name依据

Table names

To save you time, Django automatically derives the name of the database table from the name of your model class and the app that contains it. A model's database table name is constructed by joining the model's "app label" -- the name you used inmanage.py startapp-- to the model's class name, with an underscore between them.

For example, if you have an app bookstore (as created bymanage.pystartapp bookstore), a model defined asclass Book will have a database table namedbookstore_book.

To override the database table name, use the db_table parameter inclassMeta.

If your database table name is an SQL reserved word, or contains characters that aren't allowed in Python variable names -- notably, the hyphen -- that's OK. Django quotes column and table names behind the scenes.

 

为节省您的时间,Django自动重写了数据库表,包含你的model class和app名称。一个model's数据表命名是由app label构建的。使用manage.py startapp命令去创建model's class名称,并在他们之间加上下划线。

例如,如果你有一个app叫bookstore(就像是使用manage.py startapp bookstore命令创建一个app一样),还有一个model定义为class Book,那么这个数据表就叫做bookstore_book。

如果要重写这个数据表名称,在class Meta中使用db_table参数。

如果你的数据表名称是一个SQL的保留字,或者是包含一个在Python变量名中的字符是不被允许使用的,这里特别说明一下,连字符是可以的。因为Django引用列和表名称是在幕后进行的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值