AbstractUser Django完整的例子

时间:2014-02-14 12:18:11

标签: django python-2.7 django-models django-users django-1.5

我是Django的新手,我已经尝试了几周,但找不到解决这个问题的方法。

我想存储其他信息,例如用户手机号码,银行名称,银行帐户。并希望在用户注册时存储手机号码,并希望用户使用(手机号码和密码)或(电子邮件和密码)登录。

这是我的UserProfile模型

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

有人可以帮我这个或者指出完整的工作原理,我看过Django文档,但没有找到任何运气。或者,如果有另一种方法可以做到这一点。

提前致谢

编辑1:

从注册表格注册时,我也收到此错误

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

2 个答案:

答案 0 :(得分:4)

你在这里有点困惑。将AbstractUser子类化并将AUTH_USER_MODEL定义为子类 - 的想法是新模型完全取代了auth.models.User。您根本不应该导入原始用户,您当然应该调用User.objects.create_user():您的新模型管理器现在有自己的create_user方法。

因此,没有理由使用内联管理员。您的UserProfile应使用现有的django.contrib.auth.admin.UserAdmin类在管理员中注册。

答案 1 :(得分:1)

内联表单假设您的模型上有一个Generic ForeignKey,在这种情况下,UserProfileAdmin需要UserProfile的Generic ForeignKey,它不存在。尝试定期进行模型管理,例如:

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

admin.site.register(UserProfile, UserProfileAdmin)
相关问题