简体   繁体   English

Django如何自定义表名和外键字段

[英]Django how to customize table name and foreign key field

Working with existing database. 使用现有数据库。 All existing tables has named space prefixed tbl_ . 所有现有表的名称空间都以tbl_为前缀。 How can I create model class or configure django settings to prefix tbl_ before querying or syncing. 如何在查询或同步之前创建模型类或将Django设置配置为前缀tbl_

Also each child table having foreign key on fields, each field has prefixed with name space id_[table_name] . 同样,每个子表在字段上都具有外键,每个字段都以名称空间id_[table_name]为前缀。 I want to configure for foreign key too. 我也想配置外键。

Configure your models to use the table and column names you want. 配置模型以使用所需的表名和列名。 Here's an example: 这是一个例子:

class Category(models.Model):
    name = models.CharField('Name', max_length=255)

    class Meta:
        db_table = 'tbl_category'

class Entry(models.Model):
    category = models.ForeignKey(Category, db_column='id_tbl_category')
    contents = models.TextField('Contents')

    class Meta:
        db_table = 'tbl_entry'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM