2017-08-12 99 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表。或者如果失去它們對你來說不是問題。