当前位置:首页 >> 脚本专栏

Django修改app名称和数据表迁移方案实现

项目一开始的设计很重要,django中app的名称建议用小写

我的博客由两个app组成,Blog和JiaBlog,总觉得不美观,想改成小写的oblog和blog,那么最关键的地方在于和orm数据表的映射

把JiaBlog.articles改成blog.articles是终极目标,这里不推荐用django的migrate

写一个sql批量生成mysql改表名的语句

SELECT
 CONCAT(
 'ALTER TABLE ',
 table_name,
 ' RENAME TO b',
 substring(table_name, 5),
 ';'
 )
FROM
 information_schema.tables Where table_name LIKE 'JiaBlog_%';

这样就批量生成了sql语句

ALTER TABLE JiaBlog_Love RENAME TO blog_Love;
ALTER TABLE JiaBlog_area RENAME TO blog_area;
ALTER TABLE JiaBlog_articlebodypic RENAME TO blog_articlebodypic;
ALTER TABLE JiaBlog_articles RENAME TO blog_articles;
ALTER TABLE JiaBlog_articles_tags RENAME TO blog_articles_tags;
ALTER TABLE JiaBlog_blogauth RENAME TO blog_blogauth;
ALTER TABLE JiaBlog_blogrole RENAME TO blog_blogrole;
ALTER TABLE JiaBlog_bloguser RENAME TO blog_bloguser;
ALTER TABLE JiaBlog_bloguser_role RENAME TO blog_bloguser_role;
ALTER TABLE JiaBlog_blogusercollect RENAME TO blog_blogusercollect;
ALTER TABLE JiaBlog_category RENAME TO blog_category;
ALTER TABLE JiaBlog_codemodel RENAME TO blog_codemodel;
ALTER TABLE JiaBlog_comment RENAME TO blog_comment;
ALTER TABLE JiaBlog_daynumber RENAME TO blog_daynumber;
ALTER TABLE JiaBlog_genre RENAME TO blog_genre;
ALTER TABLE JiaBlog_graduation RENAME TO blog_graduation;
ALTER TABLE JiaBlog_hits RENAME TO blog_hits;
ALTER TABLE JiaBlog_honour RENAME TO blog_honour;
ALTER TABLE JiaBlog_jia RENAME TO blog_jia;
ALTER TABLE JiaBlog_jiafile RENAME TO blog_jiafile;
ALTER TABLE JiaBlog_message RENAME TO blog_message;
ALTER TABLE JiaBlog_movie RENAME TO blog_movie;
ALTER TABLE JiaBlog_note RENAME TO blog_note;
ALTER TABLE JiaBlog_paper RENAME TO blog_paper;
ALTER TABLE JiaBlog_params RENAME TO blog_params;
ALTER TABLE JiaBlog_project RENAME TO blog_project;
ALTER TABLE JiaBlog_recruinfo RENAME TO blog_recruinfo;
ALTER TABLE JiaBlog_recruitment RENAME TO blog_recruitment;
ALTER TABLE JiaBlog_resume RENAME TO blog_resume;
ALTER TABLE JiaBlog_sysrecord RENAME TO blog_sysrecord;
ALTER TABLE JiaBlog_tag RENAME TO blog_tag;
ALTER TABLE JiaBlog_teacher RENAME TO blog_teacher;
ALTER TABLE JiaBlog_userip RENAME TO blog_userip;
ALTER TABLE JiaBlog_version RENAME TO blog_version;
ALTER TABLE JiaBlog_visitnumber RENAME TO blog_visitnumber;

然后其余步骤大概如下
1.修改文件夹名称
2.修改app中views.py内关于models的引用
3.修改app中urls.py内关于app的views.py的引用
4.修改项目settings.pyapp名称
5.修改项目urls.py中include的app名称