我是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")
你能否提供一个链接,我可以通过注册表单和视图查看Django AbstractUser的完整实现。 我从2周来到这里找不到出路。 – vaibhav1312
关注此:[link](https://wsvincent.com/django-custom-user-model-tutorial/) –