我想在django allauth中保存自定义字段。我创建了模型和表单来创建单独的表,我可以在该表中保存user_id
。但我无法在该表中保存实际的自定义字段。该表由UserProfile模型表示myApp_userprofile
。
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import *
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile')
school = models.CharField(max_length=128, blank=False)
def __unicode__(self): # __str__
return unicode(self.user.username)
def create_user_profile(sender, instance=None, created=False, **kwargs):
if created:
profile, created = UserProfile.objects.get_or_create(user=instance)
post_save.connect(create_user_profile, sender=User)
当我保存表单时,它只保存user_id
而没有任何错误,但学校字段为空。能不能让我知道我想念的是什么?
是的,我是蟒蛇的超级新手
forms.py
from django import forms
from .models import UserProfile
SCHOOL = (
('', 'Select your school...'),
('ucla.edu', 'UCLA'),
('berkeley.edu', 'Berkeley'),
('gmail.com', 'Test Gmail'),
)
class SignupForm(forms.Form):
school = forms.ChoiceField(choices=SCHOOL, required=True)
def signup(self, request, user):
profile = UserProfile()
profile.school = self.cleaned_data['school']
#profile.save(profile, update_fields=['school'])
profile.create_user_profile(profile)
signup.html
<form id="signup_form" method="post" action="{% url 'account_signup' %}">
{% csrf_token %}
{{ form|crispy }}
{% if form.non_field_errors %}
<div class="alert alert-warning">
<ul class="alert-message">
{% for error in form.non_field_errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
</div>
{% endif %}
{% if redirect_field_value %}
<input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />
{% endif %}
<div class="footer text-center">
<button class='btn btn-primary btn-raised btn-lg' type="submit" id="btnSignUp">{% trans "Sign Up" %}</button>
</div>
</form>
提前致谢!!!