Django Allauth没有保存自定义表单

时间:2014-06-20 11:56:51

标签: 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 个答案:

答案 0 :(得分:0)

好的,经过一段时间后我自己解决了表格注册def:

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()