#django #django-forms #django-allauth
#django #django-forms #django-allauth
Вопрос:
Я добавляю поля для регистрации, пользователь создается и сохраняется, но данные не поступают в базу данных. Пользовательской модели пользователя не существует. Дополнительные поля переходят в другую таблицу, это другая модель.
settings.py
SOCIALACCOUNT_AUTO_SIGNUP = False
ACCOUNT_SIGNUP_FORM_CLASS = 'myapp.forms.UserDataForm'
LOGIN_REDIRECT_URL = '/'
SOCIALACCOUNT_QUERY_EMAIL = True
SOCIALACCOUNT_PROVIDERS = {
'facebook': {
'SCOPE': ['email'],
'METHOD': 'js_sdk' # instead of 'oauth2'
}
}
models.py
from django.contrib.auth.models import User
from django.db import models
from django import forms
class UserData(models.Model):
user = models.OneToOneField(User)
year = models.CharField(max_length=4)
month = models.CharField(max_length=2)
day = models.CharField(max_length=2)
hour = models.CharField(max_length=2)
minute = models.CharField(max_length=2)
sec = models.CharField(max_length=2)
latdeg = models.CharField(max_length=2)
latmin= models.CharField(max_length=2)
londeg = models.CharField(max_length=2)
lonmin= models.CharField(max_length=2)
def __unicode__(self):
return self.user.username
forms.py
from myapp.models import UserData
from django.contrib.auth.models import User
from django import forms
from django.forms.widgets import HiddenInput
class UserDataForm(forms.ModelForm):
class Meta:
model = UserData
fields = ('year','month','day','hour','minute','sec','latdeg','latmin','londeg','lonmin')
def signup(self,request, user):
user.username = user.username
user.year = self.cleaned_data['year']
user.month = self.cleaned_data['month']
user.day = self.cleaned_data['day']
user.hour = self.cleaned_data['hour']
user.minute = self.cleaned_data['minute']
user.sec = self.cleaned_data['sec']
user.latdeg = self.cleaned_data['latdeg']
user.latmin = self.cleaned_data['latmin']
user.londeg = self.cleaned_data['londeg']
user.lonmin = self.cleaned_data['lonmin']
user.save()
Ответ №1:
Хорошо, решил сам, через некоторое время я сделал это с помощью определения регистрации формы:
def signup(self,request,user):
user=User.objects.get(email=request.email)
year = request.POST.get('year', '')
month = request.POST.get('month', '')
day = request.POST.get('day', '')
hour = request.POST.get('hour', '')
minute = request.POST.get('minute', '')
sec = request.POST.get('sec', '')
latdeg = request.POST.get('latdeg', '')
latmin = request.POST.get('latmin', '')
londeg = request.POST.get('londeg', '')
lonmin = request.POST.get('lonmin', '')
userdata_obj = UserData(user=user,year=year, month=month, day=day, hour=hour,minute=minute, sec=sec,latdeg=latdeg,latmin=latmin,londeg=londeg,lonmin=lonmin)
userdata_obj.save()