Saving a custom model using django-allauth

I have a django MyUser user user model with one additional field:

 # models.py from django.contrib.auth.models import AbstractUser class MyUser(AbstractUser): age = models.PositiveIntegerField(_("age")) # settings.py AUTH_USER_MODEL = "web.MyUser" 

I also have custom all-auth registration form classes for these instructions :

 # forms.py class SignupForm(forms.Form): first_name = forms.CharField(max_length=30) last_name = forms.CharField(max_length=30) age = forms.IntegerField(max_value=100) class Meta: model = MyUser def save(self, user): user.first_name = self.cleaned_data['first_name'] user.last_name = self.cleaned_data['last_name'] user.age = self.cleaned_data['age'] user.save() # settings.py ACCOUNT_SIGNUP_FORM_CLASS = 'web.forms.SignupForm' 

After sending SignupForm (the field for the MyUser.age property MyUser.age displayed differently), I get this error:

IntegrityError at / accounts / signup /
(1048, "Column" age "cannot be zero")

What is the correct way to store a user model of a user?

django-allauth: 0.12.0; django: 1.5.1; Python 2.7.2

+4
source share
3 answers

Although it's a bit late, but in case it helps someone.

You need to create your own custom account adapter by subclassing DefaultAccountAdapter and setting

 class UserAccountAdapter(DefaultAccountAdapter): def save_user(self, request, user, form, commit=True): """ This is called when saving user via allauth registration. We override this to set additional data on user object. """ # Do not persist the user yet so we pass commit=False # (last argument) user = super(UserAccountAdapter, self).save_user(request, user, form, commit=False) user.age = form.cleaned_data.get('age') user.save() 

and you also need to define the following settings:

 ACCOUNT_ADAPTER = 'api.adapter.UserAccountAdapter' 

It is also useful if you have a custom RegisterForm function for creating other models during user registration, and you need to make an atomic transaction that would prevent any data from being stored in the database if all of them fail.

DefaultAdapter for django-allauth saves the user, so if you have an error in the save method of your RegisterForm custom parameter, the user will still be saved in the database.

So, for those facing this problem, your CustomAdpater will look like this:

class UserAccountAdapter (DefaultAccountAdapter):

  def save_user(self, request, user, form, commit=False): """ This is called when saving user via allauth registration. We override this to set additional data on user object. """ # Do not persist the user yet so we pass commit=False # (last argument) user = super(UserAccountAdapter, self).save_user(request, user, form, commit=commit) user.age = form.cleaned_data.get('age') # user.save() This would be called later in your custom SignupForm 

Then you can decorate your own RegisterForm file with @transaction.atomic

 @transaction.atomic def save(self, request, user): user.save() #save the user object first so you can use it for relationships ... 
+6
source

Side note

With the Django 1.5 custom model, the best practice is to use the get_user_model function:

 from django.contrib.auth import get_user_model # forms.py class SignupForm(forms.Form): first_name = forms.CharField(max_length=30) last_name = forms.CharField(max_length=30) age = forms.IntegerField(max_value=100) class Meta: model = get_user_model() # use this function for swapping user model def save(self, user): user.first_name = self.cleaned_data['first_name'] user.last_name = self.cleaned_data['last_name'] user.age = self.cleaned_data['age'] user.save() # settings.py ACCOUNT_SIGNUP_FORM_CLASS = 'web.forms.SignupForm' 

It may not be connected, but I thought it was worth noting.

+1
source

I think you should define the property of the fields in the Meta class in RegistrationForm and set the list of fields that contain age, for example:

 class SignupForm(forms.Form): ... class Meta: model = MyUser fields = ['first_name', 'last_name', 'age'] 

and if it doesn’t work, look at this

0
source

Source: https://habr.com/ru/post/1491288/


All Articles