数据库中的Django模型字段注释 [英] django model field comment in database

查看:1662
本文介绍了数据库中的Django模型字段注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是模型类(Django 2.0版)

this is the model class(django version 2.0)

class Host(models.Model):
    host_id=models.CharField(max_length=20,primary_key=True)
    host_label=models.CharField(verbose_name="linux_host_label",max_length=255)

数据库中的表结构

mysql> show create table dashboard_host;
| dashboard_host | CREATE TABLE `dashboard_host` (
  `host_id` varchar(20) NOT NULL,
  `host_label` varchar(255) NOT NULL,
  PRIMARY KEY (`host_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 |

但是在模型类中如何获得这样的结果

but how to do in the model class to get like this

mysql> show create table dashboard_host;
| dashboard_host | CREATE TABLE `dashboard_host` (
  `host_id` varchar(20) NOT NULL COMMENT '主机id',
  `host_label` varchar(255) NOT NULL COMMENT '主机标签',
  PRIMARY KEY (`host_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 |
+----------------+-----------------------------------


推荐答案

在django中直接从模型中获取这种方式。您可以检票 https://code.djangoproject.com/ticket/24638

well there is no such way in django from the model directly. you can check the ticket https://code.djangoproject.com/ticket/24638

,但您可以做一件事,

首先然后编辑迁移文件并放置

first makemigrations your app and then edit the migration file and place

  CREATE TABLE `dashboard_host` (
  `host_id` varchar(20) NOT NULL COMMENT '主机id',
  `host_label` varchar(255) NOT NULL COMMENT '主机标签',
  PRIMARY KEY (`host_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4

代替django自动生成的sql查询

inplace of the sql query automatically generated by django

然后迁移

这篇关于数据库中的Django模型字段注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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