如何使用Django重命名模型

如何使用Django重命名模型

本文介绍了如何使用Django重命名模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这个模型:

  class Foo(models.Model):
名称=模型。 CharField(max_length = 255)

我想将其重命名为 Bar 。我应该采取什么步骤?以及如何处理以下内容类型是过时的提示符?

解决方案

在我的特定情况下,是这样的:


  1. 重命名模型类名称及其所有引用。

  2. 运行 ./ manage.py makemigrations

  3. 检查数据库中应用程序自带表的引用。普通引用和。

  4. 检查数据库中是否有来自Django表的引用。

      my_table-+-django_admin_log 
    `-auth_permission-+-auth_group_permissions
    `-auth_user_user_permissions

    在我的情况下,我在 django_admin_log auth_group_permissions auth_user_user_permissions


  5. 运行 ./ manage.py迁移。当它询问时:

     以下内容类型是陈旧的,需要删除:

    myapp | foo

    通过外键与这些内容类型相关的任何对象也将被删除
    。您确定要删除这些内容类型吗?
    如果您不确定,请回答否。

    键入是继续,或否取消:

    只有在上述三个表中的 foo 表中都没有引用记录的记录时,您才能回答。或者,如果丢失对您而言不是问题。



Let's say I have this model:

class Foo(models.Model):
    name = models.CharField(max_length=255)

I want to rename it to Bar. What steps should I take? And how do I deal with The following content types are stale prompt?

解决方案

In my particular case it was like so:

  1. Rename model class name and all its references.
  2. Run ./manage.py makemigrations.
  3. Inspect your database for references from tables your apps own. Ordinary references and references via django_content_type table. Deal with them accordingly.
  4. Inspect your database for references from Django tables. Most likely that would be:

    my_table-+-django_admin_log
             `-auth_permission-+-auth_group_permissions
                               `-auth_user_user_permissions
    

    In my case I had no records in django_admin_log, auth_group_permissions, auth_user_user_permissions.

  5. Run ./manage.py migrate. When it asks:

    The following content types are stale and need to be deleted:
    
        myapp | foo
    
    Any objects related to these content types by a foreign key will also
    be deleted. Are you sure you want to delete these content types?
    If you're unsure, answer 'no'.
    
        Type 'yes' to continue, or 'no' to cancel:
    

    You can answer yes only if you have no records referencing records in foo table in the three tables mentioned above. Or if losing them is not an issue for you.

这篇关于如何使用Django重命名模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 05:27