I\'m trying to integrate django-allauth with a custom user model (subclassed AbstractUser, but when I test the signup form I get an integrity error due to field (date_of_bir
The answer -- which I'm still figuring out -- seems to be that if you are saving a model that contains field types that allauth.account.adapter.DefaultAccountAdapter
doesn't handle correctly (e.g. any field that lacks a __getitem__
attribute, like models.DateField
) it is necessary to implement a custom adapter somewhat like below.
note: your subclassed abstract user model is the user that's passed in, so the best practice is to use the form data directly like user.email = data.get('email')
rather than using the allauth internal methods used in the DefaultAccountAdapter class
class AccountAdapter(DefaultAccountAdapter):
def save_user(self, request, user, form, commit=False):
data = form.cleaned_data
user.email = data.get('email')
user.username = data.get('username')
# all your custom fields
user.date_of_birth = data.get('date_of_birth')
user.gender = data.get('gender')
if 'password1' in data:
user.set_password(data["password1"])
else:
user.set_unusable_password()
self.populate_username(request, user)
if commit:
user.save()
return user