我尝试使用django 1.11 cookiecutter开箱即用的all-auth实现两种用户类型。 我在这里尝试了解决方案Multiple user type sign up with django-allauth
但寻求者类型一对一的关系永远不会只创建用户。
之前有人实现过吗?
TIA
模型
from django.contrib.auth.models import AbstractUser
from django.core.urlresolvers import reverse
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
import config.settings.base as settings
@python_2_unicode_compatible
class User(AbstractUser):
# First Name and Last Name do not cover name patterns
# around the globe.
name = models.CharField(_('Name of User'), blank=True, max_length=255)
def __str__(self):
return self.username
def get_absolute_url(self):
return reverse('users:detail', kwargs={'username': self.username})
class Seeker(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
user_location = models.ForeignKey('location.Location', null=True, blank=True)
class Meta:
verbose_name = "Seeker"
verbose_name_plural = "Seekers"
def __str__(self):
return self.user.username
表格
from allauth.account.forms import SignupForm
from .models import Seeker, Employer
class SeekerSignUpForm(SignupForm):
def save(self, request):
# Save the User instance and get a reference to it
user_seeker = super(SeekerSignUpForm, self).save(request)
# Create an instance of your model with the extra fields
# then save it.
# (N.B: the are already cleaned, but if you want to do some
# extra cleaning just override the clean method as usual)
seeker_user = Seeker.objects.get_or_create(user=user_seeker)
seeker_user.save()
return seeker_user.user
查看
class SeekerSignupView(SignupView):
template_name = 'account/signup_seeker.html'
form_class = SeekerSignUpForm
view_name = 'seeker_signup'