如何 link django 自定义用户模型和 django allauth 包

How to link django custom user model and django allauth package

我在 django 中有一个使用 AbstractUser 制作的自定义用户模型。我正在使用 django allauth 包进行身份验证。我在两者中都创建了自定义字段,但无法 link 它们。

models.py

from django.db import models
from django.conf import settings
from django.shortcuts import reverse
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    age = models.CharField(max_length=30, blank=True)
    phone = models.CharField(max_length=30, blank=True)
    address = models.CharField(max_length=30,blank=True)
    gender = models.CharField(max_length=30,blank=True)

forms.py

from allauth.account.forms import SignupForm
from django.contrib.auth import get_user_model
from .models import User
class MyCustomSignupForm(SignupForm):

    first_name = forms.CharField(max_length=30, label='First Name')
    last_name = forms.CharField(max_length=30, label='Last Name')
    age = forms.CharField(max_length=30, label='Age')
    phone = forms.CharField(max_length=30, label='Phone Number')
    address = forms.CharField(max_length=30, label='Address')
    gender = forms.CharField(max_length=30,label = 'Gender')
    print('FN',first_name)
    print('Age',age)

    def signup(self, request, user):
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        user.age = self.cleaned_data['age']
        user.phone = self.cleaned_data['phone']
        user.address = self.cleaned_data['address']
        user.gender = self.cleaned_data['gender']
        user.save()
        return user

我已经定义了一个 customUserAdmin

admin.py

from django.contrib import admin
from .models import Medicine,Order,OrderItem,Pharmacy,Address
from django.contrib.auth.admin import UserAdmin
from .models import User
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth import get_user_model


class CustomUserAdmin(UserAdmin):
    """Define admin model for custom User model with no username field."""
    fieldsets = (
        (None, {'fields': ('email', 'password')}),
        (_('Personal info'), {'fields': ('first_name', 'last_name','age','phone','address','gender')}),
        (_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
                                       'groups', 'user_permissions')}),
        (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
    )
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('email', 'password1', 'password2'),
        }),
    )
    list_display = ('email', 'first_name', 'last_name', 'is_staff')
    search_fields = ('email', 'first_name', 'last_name')
    ordering = ('email',)
admin.site.register(get_user_model(), CustomUserAdmin)

我已经在 settings.py 中使用

注册了我的自定义用户模型
AUTH_USER_MODEL = 'core.User'

在 django allauth 注册页面和自定义用户模型中都显示了字段。但是当我注册并在自定义字段中添加值时,它们不会存储在数据库的用户模型中。我无法 link 注册页面和用户模型。

使用 Django 版本 3.1.7

这是一种将用户类型保存到数据库记录的方法

forms.py

from django import forms
from allauth.account.forms import SignupForm
from .models import User

class MyCustomSignupForm(SignupForm):

    def __init__(self, *args, **kwargs):
        super(MyCustomSignupForm, self).__init__(*args, **kwargs)
        first_name = forms.CharField(max_length=30, label='First Name')
        last_name = forms.CharField(max_length=30, label='Last Name')
        age = forms.CharField(max_length=30, label='Age')
        phone = forms.CharField(max_length=30, label='Phone Number')
        address = forms.CharField(max_length=30, label='Address')
        gender = forms.CharField(max_length=30,label = 'Gender')

    # Put in custom signup logic
    def custom_signup(self, request, user):
        # Set the user's type from the form reponse
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        user.age = self.cleaned_data['age']
        user.phone = self.cleaned_data['phone']
        user.address = self.cleaned_data['address']
        user.gender = self.cleaned_data['gender']

        # Save the user's type to their database record
        user.save()

如果不起作用,请看这个: https://dev.to/danielfeldroy/customizing-django-allauth-signup-forms-2o1m