Django完整示例:AbstractUser

12

我是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个回答

6
你在这里有些混淆了。子类化AbstractUser并将AUTH_USER_MODEL定义为你的子类的想法是,新模型完全替换了auth.models.User。你不应该导入原始的用户(User),而且你肯定不应该调用User.objects.create_user(): 你的新模型管理器现在有自己的create_user方法。
因此,没有必要去搞定内联管理员。你的UserProfile应该使用现有的django.contrib.auth.admin.UserAdmin类在管理员中进行注册。

19
你能提供一个链接,让我可以看到Django AbstractUser的完整实现,并带有注册表单和视图吗?我已经被卡了两个星期了,找不到出路。 - vaibhav1312

2

内联表单假定您在模型上拥有一个通用外键。在这种情况下,UserProfileAdmin期望UserProfile的通用外键,但该外键不存在。尝试使用常规的模型管理器,如:

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

admin.site.register(UserProfile, UserProfileAdmin)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接