2017-08-12 55 views
1

比方说,我有这样的模式:如何用Django重命名模型?

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

我想将它重命名为Bar。我应该采取什么措施?我该如何处理The following content types are stale提示?

+0

难道你不能重命名它并运行'./manage.py makemigrations'吗? – C14L

+0

@ C14L因为你必须对Django内部有一定的了解。看到我的答案。 –

回答

1

在我的特殊情况下,它是像这样:

  1. 重新命名模型类名和它的所有引用。
  2. 运行./manage.py makemigrations
  3. 检查您的数据库中您的应用所拥有的表格的参考。普通的参考文献和references via django_content_type table。相应地处理它们。
  4. 检查您的数据库以获取来自Django表的引用。最有可能的,这将是:

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

    在我来说,我没有记录在django_admin_logauth_group_permissionsauth_user_user_permissions

  5. 运行./manage.py migrate。当它问:

    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: 
    

    你可以回答yes只有当你有没有记录上述三个表中引用记录foo表。或者如果失去它们对你来说不是问题。