Django Userena的多用户配置文件

时间:2013-07-03 03:36:10

标签: django django-users django-profiles

我正在使用userena 1.2和Django 1.4。我正在尝试创建多个用户配置文件,但我没有运气,因为在SO上发现了许多帖子。

我创建了两个自定义模型和一个这样的表单:

class BaseProfile(UserenaBaseProfile):
    """ Default profile """
    user = models.OneToOneField(User, unique=True, verbose_name = _('user'), related_name='base_profile')

    @property # accessing a method as a property
    def is_seller(self):
        """Find out if this is a Seller user"""
        try:
            self.seller
            return True
        except Seller.DoesNotExist:
            return False

    def get_profile_type(self):
        """return the profile type model"""
        if self.is_seller:
            return self.seller
        else:
            return self.customer


class Seller(BaseProfile):
    company = models.CharField(_('Company'),max_length=100, null=True, blank=True,)


class Customer(BaseProfile):
    favourite_snack = models.CharField( _('favourite snack'), max_length=5 )

并覆盖了注册表单:

class SignupFormExtra(SignupForm):
    # stuff in here
    def save(self):
        # stuff in here

现在问题出在 save 方法中 基于this answer我试图实现自定义管理器,但我没有运气(我是django新手)。我理解的重点是Save方法应该返回Django用户,而不是userena配置文件。

def save(self):
    user = super(SpamSignupForm,self).save()
    new_customer = Customer.objects.create_user()
    return new_user

然后我尝试了类似this的内容:

def save(self):
    new_user = super(SignupFormExtra, self).save()
    new_user.save()
    customer = Customer(profile = new_user.get_profile(), user=new_user)
    customer.save()
    return new_user

get_profile()方法将始终(我猜)返回settings.AUTH_PROFILE_MODULE中定义的基本配置文件。
我似乎也错了,作者使用子配置文件中的profile字段作为与 Baseprofile 的OneToOne关系实现多表继承,为什么?这对我来说似乎不对。

class Customer(Baseprofile):
    profile = models.OneToOneField(Baseprofile,
                            unique=True,
                            verbose_name=_('profile'),
                            related_name='student')

是的,基本上我花了整整一天的时间试图解决这个问题,但我仍然迷失了。

1 个答案:

答案 0 :(得分:-2)

最好使用django-registration。安装并将其添加到INSTALLED_APPS =(.....'registration',

现在在

中创建两个用户

登记/ models.py

.................

class Seller(models.Model):
    user=models.OneToOneField(User)
    companyname= models.CharField(max_length=100,blank=True)

    def __unicode__(self):
        return self.user.username

class Customer(models.Model):
    user=models.OneToOneField(User)
    birth_date = models.DateField(null=True, blank=True)
    favourite_snack = models.CharField(max_length=5)
    def __unicode__(self):
        return self.user.username

在registration / views.py

........
from django.contrib.auth import authenticate, login
from registration.models import Seller,Customer
from django.http import HttpResponse
def auth_login(request,utype):
    temp_login='registration/%slogin.html' %utype
    try:
        username = request.POST['username']
        password = request.POST['password']        
    except KeyError:
        return render(request,temp_login)
    user = authenticate(username=username, password=password)
    if utype=='seller':
        try:Seller.objects.get(user=user)
        except: return render(request,temp_login,{'errors':True})
    if utype=='customer':
        try:Customer.objects.get(user=user)
        except: return render(request,temp_login,{'errors':True})    
    if user.is_active:
        login(request,user)
        return HttpResponseRedirect('/'+request.GET['next'])#,render(request,'%s/home.html' %utype)
    return render(request,temp_login,{'errors':True})

编辑registration / auth_urls.py

urlpatterns = patterns('',
                       url(r'^login/(employer|jobseeker)/$',
                           auth_login,name='auth_login'),

登记/后端/默认/ views.py

class RegistrationView(BaseRegistrationView):

def register(self, request, **cleaned_data):内添加

if self.args[0]=='e': usertype=Seller()
        else: usertype=Customer()        
        usertype.user=new_user
        usertype.save()

登记/后端/默认/ urls.py

添加这些行

url(r'^register/([ej])/$',
                           RegistrationView.as_view(),
                           name='registration_register'),