2013-11-14 59 views
0

我想與django和南方握手,而且我似乎遇到了stale contenttype問題 - 我無法找到它在SO或谷歌上的修復。django南 - 過時的用戶模型,當使用AbstractUser

所以,開始與我有Django的== 1.6一個簡單的項目上安裝的應用程序如下:

INSTALLED_APPS = (
    'django.contrib.auth', 
    'django_browserid', # Load after auth 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.sites', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'django.contrib.sitemaps', 
    # Uncomment the next line to enable the admin: 
    'django.contrib.admin', 
    # Uncomment the next line to enable admin documentation: 
    # 'django.contrib.admindocs', 
    'south', 
) 

AUTH_USER_MODEL = 'auth.User' 

和我運行這個syncdb在這個階段不創建一個超級用戶。

現在,我創建了一個新的應用程序loginapp並創建一個AbstractUser如下:

#loginapp/models.py 
class MyUser(AbstractUser): 
    is_admin_enabled = models.BooleanField(default=True) # new field 

,並改變我的settings.py如下:

AUTH_USER_MODEL = "loginapp.MyUser" 
現在

,在登錄應用程序,我跑(我加loginapp到我的INSTALLED_APPS字典):

python manage.py schemamigration loginapp --initial && python manage.py migrate loginapp 

..到目前爲止都很好 - 我可以看到南在我的db上創建了新的用戶模型。

現在,我回去和我的項目做一個syncdb,我得到:

The following content types are stale and need to be deleted: 

    auth | user 

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'. 

..我猜Django的實現用戶模型已經改變,默認模式已經過時了。我試着在這裏使用「是」,我看到數據庫表仍然存在 - 大概是因爲syncdb不會刪除數據庫表。

我該如何避免上述問題?我只需要我的loginapp中定義的用戶模型,而不是我的數據庫上的默認django用戶模型 - 使用南方。

真的很感謝任何線索/方向來解決這個問題。

+0

這是沒有問題的。它只意味着auth |用戶不再在應用程序中引用(基本無法訪問)。南方發現它,並希望看到你想用它做什麼。如果你不打算再使用它,可以說'是'。通過說'不',表格保留在數據庫中,沒有任何傷害。 – karthikr

+0

我不確定在這裏說「是」或「否」影響結果。無論您說「是」還是「否」,表格都保留在數據庫中。 'syncdb'不能真正刪除表。我確信有一種方法可以在不出現這種「陳舊問題」的情況下進行設置。思考? – AJW

+0

我們在這裏談論南方。這與syncdb無關。比方說,你做了一個全新的應用程序安裝和一個syncdb,這些表永遠不會被創建。 – karthikr

回答

0

我遇到了類似的問題,使用Django 1.7遷移遷移auth.models.Usermyapp.User(從AbstractUser繼承),並沒有確定要刪除這與User做我現有的生產管理日誌表項,所以我堅持讓這絕對正確。

假設myappp.models是:

from django.db import models 
from django.contrib.auth.models import AbstractUser 

class User(AbstractUser): 
    class Meta: 
     db_table = 'auth_user' 

這裏是我想出了:

from django.db import models, migrations 
import django.utils.timezone 
import django.core.validators 

MYAPP = 'myapp' 

def migrate_func(old, new, apps, schema_editor): 
    ContentType = apps.get_model("contenttypes", "ContentType") 
    db_alias = schema_editor.connection.alias 
    ct = ContentType.objects.using(db_alias).get(app_label=old, model='user') 
    ct.app_label = new 
    ct.save() 

def forwards_func(apps, schema_editor): 
    migrate_func('auth', MYAPP, apps, schema_editor) 

def backwards_func(apps, schema_editor): 
    migrate_func(MYAPP, 'auth', apps, schema_editor) 


class Migration(migrations.Migration): 

    dependencies = [ 
     ... 
    ] 

    database_operations = [ 
     migrations.RunPython(forwards_func, backwards_func) 
    ] 

    state_operations = [ 
     migrations.CreateModel(
      name='User', 
      fields=[ 
       ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), 
       ... 
      ], 
      options={ 
       'db_table': 'auth_user', 
      }, 
      bases=(models.Model,), 
     ), 
    ] 

    operations = [ 
     migrations.SeparateDatabaseAndState(
      state_operations=state_operations), 
     migrations.SeparateDatabaseAndState(
      database_operations=database_operations) 
    ] 
相關問題