简体   繁体   中英

Django how to customize table name and foreign key field

Working with existing database. All existing tables has named space prefixed tbl_ . How can I create model class or configure django settings to prefix tbl_ before querying or syncing.

Also each child table having foreign key on fields, each field has prefixed with name space 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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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