2014-02-14 149 views
9

我是Django的新手,我一直在嘗試這個問題數週,但找不到解決此問題的方法。AbstractUser Django完整示例

我想存儲用戶手機號碼,銀行名稱,銀行賬戶等附加信息。並希望在用戶註冊時存儲手機號碼,並希望用戶使用(手機號碼和密碼)或(電子郵件和密碼)登錄。

這是我的用戶配置模型

from django.db import models 
from django.contrib.auth.models import User 
from django.contrib.auth.models import AbstractUser 
# Create your models here. 

class UserProfile(AbstractUser): 

    user_mobile = models.IntegerField(max_length=10, null=True) 
    user_bank_name=models.CharField(max_length=100,null=True) 
    user_bank_account_number=models.CharField(max_length=50, null=True) 
    user_bank_ifsc_code = models.CharField(max_length=30,null=True) 
    user_byt_balance = models.IntegerField(max_length=20, null=True) 

這是我forms.py

from django import forms    
from django.contrib.auth.models import User # fill in custom user info then save it 
from django.contrib.auth.forms import UserCreationForm  
from models import UserProfile 
from django.contrib.auth import get_user_model 

class MyRegistrationForm(UserCreationForm): 
    email = forms.EmailField(required = True) 
    mobile = forms.IntegerField(required=True) 



    class Meta: 
     model = UserProfile 
     fields = ('username', 'email', 'password1', 'password2','mobile')   

    def save(self,commit = False): 
     user = super(MyRegistrationForm, self).save(commit = False) 
     user.email = self.cleaned_data['email'] 
     user.user_mobile = self.cleaned_data['mobile'] 
     user.set_password(self.cleaned_data["password1"]) 

     user_default = User.objects.create_user(self.cleaned_data['username'], 
               self.cleaned_data['email'], 
               self.cleaned_data['password1']) 
     user_default.save() 

     if commit: 
      user.save() 

     return user 

在我的settings.py我已經包括

AUTH_USER_MODEL = "registration.UserProfile" 

admin.py我應用程序是

from django.contrib import admin 
from django.contrib.auth.admin import UserAdmin 
from django.contrib.auth.models import User 
from models import UserProfile 

class UserProfileInline(admin.StackedInline): 
    model = UserProfile 
    can_delete = False 
    verbose_name_plural = 'userprofile' 

class UserProfileAdmin(UserAdmin): 
    inlines = (UserProfileInline,) 

admin.site.register(UserProfile, UserProfileAdmin) 

同時加入從管理員用戶我得到這個錯誤

Exception at /admin/registration/userprofile/1/ 
<class 'registration.models.UserProfile'> has no ForeignKey to <class 'registration.models.UserProfile'> 

有人可以幫我這個或點出了完整的工作exapmle,我看到Django的文檔,但沒有發現任何運氣。或者如果有另一種方式來做到這一點。

在此先感謝

編輯1:

雖然從登記表登記,我也收到此錯誤

DatabaseError at /register 
(1146, "Table 'django_auth_db.auth_user' doesn't exist") 

回答

1

內聯形式假設你有一個通用ForeignKey的上你的模型在這種情況下,UserProfileAdmin期望UserProfile的通用ForeignKey不存在。嘗試做一個正常的模型管理,如:

class UserProfileAdmin(admin.ModelAdmin): 
    can_delete = False 
    verbose_name_plural = 'userprofile' 

admin.site.register(UserProfile, UserProfileAdmin) 
4

你在這裏困惑了一下自己。將AbstractUser進行子類化的想法 - 並將AUTH_USER_MODEL定義爲您的子類 - 是新模型完全取代了auth.models.User。您根本不應該導入原始用戶,您當然應該調用User.objects.create_user():您的新模型的管理器現在擁有自己的create_user方法。

正因爲如此,沒有任何理由不願意使用內聯管理員。您的UserProfile應該使用現有的django.contrib.auth.admin.UserAdmin類在管理員中註冊。

+16

你能否提供一個鏈接,我可以通過註冊表單和視圖查看Django AbstractUser的完整實現。 我從2周來到這裏找不到出路。 – vaibhav1312

+0

關注此:[link](https://wsvincent.com/django-custom-user-model-tutorial/) –