我是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")