简体   繁体   English

Django迁移应用程序名称的更改(活动项目)

[英]Django Migrate Change of App Name (active project)

So... I've done a lot of research on this... there are answers, but not complete or appropriate answers. 所以...我已经做了很多研究......有答案,但没有完整或适当的答案。 I have an in-use and in-production django "project" in which the "main" application is called "pages" ... for reasonably dumb reasons. 我有一个正在使用和正在生产的django“项目”,其中“主要”应用程序被称为“页面”......出于相当愚蠢的原因。 My problem is now to add mezzanine ... which has a sub-module mezzanine.pages (seems to be required .... but I'm pretty sure I need it). 我现在的问题是添加夹层...它有一个子模块mezzanine.pages(似乎是必需的....但我很确定我需要它)。

mezzanine.pages apparently conflicts with "pages" ... mezzanine.pages显然与“页面”冲突......

Now ... my pages contains a slew of non-trivial models including one that extends user (One-to-One ref), and many references to other app's tables (fortunately only outbound, ForeignKey). 现在...我的页面包含一系列非平凡的模型,包括一个扩展用户(一对一参考),以及许多对其他应用程序表的引用(幸运的是只有出站,ForeignKey)。 It also has management/commands and about 20 migrations of it's own history. 它还有管理/命令和大约20次迁移它自己的历史。

I gather I either have to changes pages to mypages or is there another route (seemingly changing mezzanine.pages seems wrong-headed). 我知道我要么必须将页面更改为mypages,要么还有另一条路径(看似改变mezzanine.pages似乎是错误的)。

for reference, The project is on Django 1.8 right now, so the preferred answer includes migrations. 供参考,该项目目前在Django 1.8上,因此首选答案包括迁移。

I've worked on this since I posted it, and the real answer is what I've synthesized from multiple sources (including other stack exchange posts). 自从我发布它以来,我一直在研究这个问题, 真正的答案是我从多个来源(包括其他堆栈交换帖子)合成的内容。

So... Everything changed in Django before I started using it. 所以......在我开始使用之前,Django的一切都发生了变化。 After 1.7, the 'migrations' bit was internalized and posts including the word "South" are about how the world was before 1.7. 在1.7之后,“迁移”位被内化,包括“南方”在内的帖子是关于1.7 之前的世界。 Further, the complication in my case dealt with the issue of migrations in that the project was already active and had real data in production. 此外,我案例中的复杂问题涉及迁移问题,因为项目已经处于活动状态,并且生产中有真实的数据。

There were some posts including a GITHub chunk of code that talked about migrating tables from one App to another App. 有一些帖子包括GITHub代码块,讨论了将表从一个应用程序迁移到另一个应用程序。 This is inherently part of the process, but several posts noted that to do this as a "migration" you needed the Migration.py to be in another App. 这本质上是该过程的一部分,但有几篇文章指出要将此作为“迁移”,您需要将Migration.py放在另一个应用程序中。 Maybe even an App created for the purpose. 也许甚至是为此目的创建的应用程序。

In-the-end, I decided to approach the problem by changing the label in the Application class of apps.py in the application in question. 最后,我决定通过更改相关Applicationapps.pyApplication类中的label来解决问题。 In my case, I am changing "pages" to "phpages" but the directory name of my app is still pages. 在我的情况下,我将“页面”更改为“phpages”,但我的应用程序的目录名仍然是页面。 This works for me because the mezzanine app's "pages" sub-App is back in the python library and not a conflict in the filesystem. 这对我有用,因为夹层应用程序的“页面”子应用程序回到python库中而不是文件系统中的冲突。 If this is not your situation, you can solve it with another use of label . 如果这不是您的情况,您可以使用另一种label来解决它。

So... Step-by-step, my procedure to rename pages to phpages . 所以...一步一步,我的程序将pages重命名为phpages

  1. Create apps.py in the pages sub-directory. pages子目录中创建apps.py In it put: 在它放:

    class PagesConfig(AppConfig): name = "pages" label = "phpages" verbose_name = "Purple Hat Pages"

    Key among these is label which is going to change things. 其中的关键是label ,它将改变一切。

  2. In __init__.py in the pages sub-directory, put default_app_config = "pages.apps.PagesConfig" pages子目录的__init__.py中,输入default_app_config = "pages.apps.PagesConfig"

  3. In your settings.py change the INSTALLED_APPS entry for your app to 'pages.apps.PagesConfig', ... 在您的settings.py ,将应用的INSTALLED_APPS条目更改为'pages.apps.PagesConfig', ...

  4. All of your migrations need to be edited in this step. 您需要在此步骤中编辑所有迁移。 In the dependencies list, you'll need to change 'pages' to 'phpages' . dependencies列表中,您需要将'pages'更改为'phpages' In the ForeignKey s you'll need to also change 'pages.Something' to 'phpages.Something' for every something in every migration file. ForeignKey是你能还需要改变'pages.Something''phpages.Something'在每一个迁移文件中的每个东西。 Find these under pages/mitrations/nnnn_*.py pages/mitrations/nnnn_*.py下找到这些

  5. If you refer to foreign keys in other modules by from pages.models import Something and then use ForeignKey(Something) , you're good for this stop. 如果你通过from pages.models import Something然后使用ForeignKey(Something) from pages.models import Something其他模块中的外键,那么你很适合这个停止。 If you use ForeignKey('pages.Something') then you need to change those references to ForeignKey('phpages.Something') . 如果你使用ForeignKey('pages.Something')那么你需要更改对ForeignKey('phpages.Something')引用。 I would assume other like-references are the same. 我认为其他类似参考也是一样的。

  6. For the next 4 steps (7, 8, 9 and 10), I built pagestophpages.sql and added it to the pages sub-directory. 在接下来的4个步骤(7,8,9和10)中,我构建了pagestophpages.sql并将其添加到pages子目录中。 It's not a standard django thing, but each test copy and each production copy of the database was going to need the same set of steps. 它不是标准的django,但每个测试副本和数据库的每个生产副本都需要相同的一组步骤。

  7. UPDATE django_contecnt_type SET app_label='phpages' WHERE app_label='pages';

  8. UPDATE django_migrations SET app='phpages' WHERE app='pages';

  9. Now... in your database (my is PostgreSQL) there will be a bunch of tables that start with "pages". 现在......在你的数据库中(我的是PostgreSQL),会有一堆以“pages”开头的表。 You need to list all of these. 您需要列出所有这些。 In PostgreSQL, in addition to tables, there will be sequences for each AutoField . 在PostgreSQL中,除了表之外,每个AutoField都有序列。 For each table construct ALTER TABLE pages_something RENAME TO phpages_something; 对于每个表构造ALTER TABLE pages_something RENAME TO phpages_something; For each sequence ALTER SEQUENCE pages_something_id_seq RENAME TO phpages_something_id_seq; 对于每个序列ALTER SEQUENCE pages_something_id_seq RENAME TO phpages_something_id_seq;

  10. You should probably backup the database. 您应该备份数据库。 You may need to try this a few times. 您可能需要尝试几次。 Run your SQL script through your database shell. 通过数据库shell运行SQL脚本。 Note that all other changes can be propagated by source code control (git, svn, etc). 请注意,所有其他更改都可以通过源代码控制(git,svn等)传播。 This last step must be run on each and every database. 必须在每个数据库上运行最后一步。

Obviously, you need to change pages and phpages to your stuff. 显然,你需要将pagesphpages更改为你的东西。 You may have more than one table with one auto field and it may not be named something . 你可能有一个汽车领域的多个表,它可能不会被命名为something

Another thing of note, in terms of process, is that this is probably a hard point in your development where everything needs be in sync. 另一方面,就流程而言,这可能是您的开发中需要同步的一个难点。 Given that we're playing with editing migrations and changing names, you need a hard stop in development so that everything that's going to be changed (dev box, test box, staging box, production box ... and all of their databases) is at the same revision and schema. 鉴于我们正在进行编辑迁移和更改名称,您需要在开发过程中进行硬停止,以便将要更改的所有内容(开发框,测试框,登台框,生产框......以及所有数据库)都是在相同的修订和架构。 YMMV. 因人而异。

This is also solving the problem by using the label field of class Application . 这也是通过使用class Applicationlabel字段来解决问题。 I choose this method in deference to changing the directory name because it involved fewer changes. 我选择此方法是为了更改目录名称,因为它涉及更少的更改。 I chose not to change the name field because that did not work for me. 我选择不更改name字段,因为这对我不起作用。 YMMV. 因人而异。

I must say that I'm a little disappointed that myapp/pages conflicts with mezzanine.pages. 我必须说myapp / pages与mezzanine.pages冲突我有点失望。 It looks like some of the reasons are due to the pages slug being used in the database table name (and off top of my head, I don't see a good solution there). 看起来有些原因是由于在数据库表名中使用了pages slug(而且在我的脑海中,我没有看到一个好的解决方案)。 What I don't see that would make sense is the equivalent to "from mezzanine import pages as mpages" or somesuch. 我认为没有意义的是相当于“从mezzanine import pages as mpages”或者其他一些东西。 The ability to alias imported apps (not talking about apps in my own file tree). 能够为导入的应用程序添加别名(不在我自己的文件树中讨论应用程序)。 I think this might be possible if I sucked in the app into my own file tree --- but this doesn't seem to be a sanctioned act, either. 我认为如果我将应用程序插入到我自己的文件树中,这可能是可能的---但这似乎也不是一个受制裁的行为。

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

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