0

I'm using userena 1.2 and Django 1.4. I'm trying to create multiple user profiles but I had no luck following many post found here on SO.

I've create 2 custom Models and a form like 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 )

And overridden the Signup form:

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

Now the problem is inside the save method.
Based on this answer I've tried to implement a custom manager but I had no luck (I'm a django newbie). The important point that I understood is that the Save method shoud return a Django User, not a userena profile.

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

Then I've tried something like 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

The get_profile() method will always (I guess) returns the base profile defined in settings.AUTH_PROFILE_MODULE.
Also it seems wrong to me that the author used a profile field in the subprofile as a OneToOne relation to the Baseprofile WHILE implementing a multi-table inheritance, why? It doesn't seem right to me.

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

Yes, basically I've spent a full day trying to figure this out and I'm still lost.

Community
  • 1
  • 1
Leonardo
  • 4,046
  • 5
  • 44
  • 85

1 Answers1

-2

it is better to use django-registration. install and add it to INSTALLED_APPS =(.....'registration',

now create two user in

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

in 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})

edit registration/auth_urls.py

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

registration/backends/default/views.py

inside class RegistrationView(BaseRegistrationView):

inside def register(self, request, **cleaned_data): add

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

registration/backends/default/urls.py

add these line

url(r'^register/([ej])/$',
                           RegistrationView.as_view(),
                           name='registration_register'),
suhailvs
  • 20,182
  • 14
  • 100
  • 98
  • I actually wanted to use userena in order to use all its other features that would take time to rewrite by myself (user account creation and editing, mugshot, messaging, visibility of the account from other users). Any alternative to implement these mixing django-registration with other reusable apps? – Leonardo Jul 03 '13 at 11:10
  • actually these codes are not built in in django-registration. you have better not use django-registraion. try to impelement these codes like editing urls.py, views.py ..etc in userena – suhailvs Jul 03 '13 at 11:27
  • That is exactly what I'm trying to do but I'm stuck in the phase of creating multiple profiles in userena. – Leonardo Jul 03 '13 at 12:34