2013-06-20 60 views
0

使用Django 1.5,嘗試自定義用戶功能。Django不爲我的自定義用戶模型創建表格

我只有3個模型在項目中。在這裏,他們是,

class CustomUser(AbstractBaseUser, PermissionsMixin): 
class CustomUserManager(BaseUserManager): 
class testModel (models.Model): 

當我做python manage.py validate沒有錯誤。當我做python manage.py sqlall lancer(藍瑟是應用程序的名稱),它顯示了以下

BEGIN; 
CREATE TABLE "lancer_testmodel" (
    "id" serial NOT NULL PRIMARY KEY, 
    "first_name" varchar(30) NOT NULL, 
    "last_name" varchar(30) NOT NULL 
) 
; 

COMMIT; 

發生了什麼事的SQL代碼爲其他兩個型號?它是否僅爲明確繼承models.Model的模型創建表?

更多信息,

  1. 我每Django文檔
  2. 我settings.py中註釋掉所有其他安裝的應用程序我 settings.py文件的末尾添加AUTH_USER_MODEL = 'lancer.CustomUser'。我知道 他們中的很多人被contrib.auth使用,但由於我使用的是新的自定義用戶模型 ,我只是評論了其他所有內容。

這是它看起來像現在,

INSTALLED_APPS = (
    #'django.contrib.auth', 
    #'django.contrib.contenttypes', 
    #'django.contrib.sessions', 
    #'django.contrib.sites', 
    #'django.contrib.messages', 
    #'django.contrib.staticfiles', 
    # Uncomment the next line to enable the admin: 
    # 'django.contrib.admin', 
    # Uncomment the next line to enable admin documentation: 
    # 'django.contrib.admindocs', 
    'lancer', 
) 
+0

它添加到您的設置變量'AUTH_USER_MODEL'? –

+0

@VictorCastilloTorres是的。我在settings.py文件的末尾添加了AUTH_USER_MODEL ='lancer.CustomUser'。 – reedvoid

+0

我想你已經在你的'INSTALLED_APPS'類似'myapp_where_my_model_is' –

回答

4

PermissionsMixin依賴的

'django.contrib.auth', 
'django.contrib.contenttypes', 

,如果你對此有何評論僅django.contrib.auth並取消django.contrib.contenttypes, 你會有這個錯誤

CommandError: One or more models did not validate: users.myuser: 'groups' has an m2m relation with model <class 'django.contrib.auth.models.Group'> , which has either not been installed or is abstract. users.myuser: 'user_permissions' has an m2m relation with model <class 'django.contrib.auth.models.Permission'> , which has either not been installed or is abstract.

如果您取消註釋django.contrib.auth和評論django.contrib.contenttypes

你也會有這種錯誤

CommandError: One or more models did not validate: auth.permission: 'content_type' has a relation with model < class 'django.contrib.contenttypes.models.ContentType'> , which has either not been installed or is abstract.

,如果你取消註釋兩個sqlall具有生成你的表:d

+0

謝謝。實際上,我發現我的代碼出現了1個更重要的錯誤(我應該完整地發佈它,但是這麼長時間......)。由於我從Django現有的contrib.auth.Abs​​tractUser複製了大部分代碼,我也複製了這段代碼:'class Meta: verbose_name = _('user') verbose_name_plural = _('users') abstract = True ',這顯然告訴Django這不是一個真正的模型或什麼,因爲它有這個「抽象=真」的東西。刪除後,和你建議的變化,它的工作!謝謝!! – reedvoid

+0

不客氣的人:D –

+1

Yeah man抽象意思是這個模型不會被用來創建任何數據庫表格,正如文檔所述(https://docs.djangoproject.com/en/dev/topics/db/models /#abstract-base-classes) –

相關問題