2014-02-14 144 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/) –