Django表命名约定。我可以改变行为吗? [英] Django table naming convention. Can I change its behavior?

查看:111
本文介绍了Django表命名约定。我可以改变行为吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当Django创建表格时,它会给它们表单app_class的名称。我正在将一个不同的(但基本相似的内容)数据库改装为Django安装。我的表名不是用app _。\\ / p>

我可以相应地重新创建我的数据库和它的表,但我想看看Django是否有灵活性来修改它的处理方式表名称。



那就是我有一个表coi_fs - 我想看看我是否可以改变Django安装,这样它不会指向app_coi_fs而是简单的coi_fs?

解决方案

如果您已经有数据库,我建议使用数据库内省选项。这将创建使用当前数据库所需的模型。

  $ django-admin.py inspectdb> models.py 

为了回答您的原始问题,请从文档(https://docs.djangoproject .com / en / dev / ref / models / options /#table-names),可以使用db_table元属性。



models.py



  class DjangoSite(models.Model) 
domain = models.CharField(max_length = 100)
name = models.CharField(max_length = 50)
class Meta:
db_table = u'site'


When Django creates tables it give them names of form app_class. I'm in the position of retrofitting a different (but basically similar in content) database to a Django installation. My tables name are not prepended with app_.

I could recreate my database and its tables accordingly but I'd like to see if Django has the flexibility to modify how it handles table names.

That is, I have a table coi_fs - I'd like to see if I can change the Django installation such that it will refer not to app_coi_fs but simply coi_fs?

解决方案

If you already have a database I would recommend using the database introspection option. This will create the models needed to use your current database as is.

$ django-admin.py inspectdb > models.py

To answer your original question though, from the docs (https://docs.djangoproject.com/en/dev/ref/models/options/#table-names), you can use the db_table meta property.

models.py

class DjangoSite(models.Model):
    domain = models.CharField(max_length=100)
    name = models.CharField(max_length=50)
    class Meta:
        db_table = u'site'

这篇关于Django表命名约定。我可以改变行为吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆