Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1100585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:54:13+00:00 2026-05-17T00:54:13+00:00

My models.py: USER_TYPES = ( (‘D’, ‘Demo’ ), (‘F’, ‘Free’ ), (‘P’, ‘Premium’), )

  • 0

My models.py:

USER_TYPES = (                                                                                                                                                                   
    ('D', 'Demo'   ),                                                                                                                                                               
    ('F', 'Free'   ),
    ('P', 'Premium'),                                                                                                                                                        
)                                                                                                                                                                                                                                                                                                                                                                

class BaseProfile(models.Model):                                                                                                                                                 
    user      = models.OneToOneField(User, primary_key=True)                                                                                                                     
    user_type = models.CharField(max_length=1, blank=True, choices=USER_TYPES)                                                                                                           

class DemoProfile(models.Model):                                                                                                                                                 
    user      = models.OneToOneField(User, primary_key=True)                                                                                                                     
    demo      = models.CharField(max_length=10, blank=True) 
    ...

class FreeProfile(models.Model):                                                                                                                                                 
    user      = models.OneToOneField(User, primary_key=True)                                                                                                                     
    free      = models.CharField(max_length=10, blank=True) 
    ...

class PremiumProfile(models.Model):                                                                                                                                                 
    user      = models.OneToOneField(User, primary_key=True)                                                                                                                     
    premium    = models.CharField(max_length=10, blank=True) 
    ...

class ProxyProfile(BaseProfile):                                                                                                                                                 
    class Meta:                                                                                                                                                                  
        proxy = True             
    def get_profile(self):                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
        if self.user_type == 'D':                                                                                                                                              
            return DemoProfile._default_manager.get(user__id__exact=self.user_id)                                                                                              
        elif self.user_type == 'F':                                                                                                                                            
            return FreeProfile._default_manager.get(user__id__exact=self.user_id)                                                                                              
        else:                                                                                                                                                                  
            return PremiumProfile._default_manager.get(user__id__exact=self.user_id)                                                                                                         

I use BaseProfile to map user_id to specific user_type. I wanted to use ProxyProfile as proxy which loads user_type depending profiles to ModelForm as shown below

Content of my forms.py:

class ProfileForm(ModelForm):                                                                                                                                                    
...                                                                                                                                                                                 
    class Meta:                                                                                                                                                                 
        model   = ProxyProfile                                                                                                                                                  
        exclude = ('user','user_type')   
...

ProfileForm is provided to django-profiles using following code in urls.py:

urlpatterns += patterns('',                                                                                                                                                      
    url(r'^profiles/edit/', edit_profile,                                                                                                                                        
        {'form_class': ProfileForm},                                                                                                                                             
        name='profiles_edit_profile'),                                                                                                                                           
    (r'^profiles/',include('profiles.urls')),                                                                                                                                    
)

I’ve also set in settings.py:

AUTH_PROFILE_MODULE = 'main.ProxyProfile'

During user registration all db data is filled correctly (it looks like everything is OK).
I register using form passed to django-registration:

urlpatterns += patterns('',                                                                                                                                                      
    url(r'^register/$', register,                                                                                                                                                
        {'form_class': UserRegistrationForm},                                                                                                                                    
        name='registration.views.register'),                                                                                                                                     
    (r'', include('registration.urls')),                                                                                                                                         
) 

from forms.py:

class UserRegistrationForm(RegistrationFormUniqueEmail, RegistrationFormTermsOfService):                                                                                         
    utype        = forms.ChoiceField(choices=USER_CHOICES)                                                                                               

    def save(self, profile_callback=None):                                                                                                                                       
        new_user = RegistrationProfile.objects.create_inactive_user(username=self.cleaned_data['username'],
                                                                    password.self.cleaned_data['password1'],                                                                     
                                                                    email=self.cleaned_data['email'],                                                                            
                                                                    )                                                                                                            
        new_base_profile = BaseProfile(user=new_user, user_type=self.cleaned_data['utype'])                                                                                      
        if self.cleaned_data['utype'] == "D":                                                                                                                                    
            new_profile = DemoProfile(user=new_user)                                                                                                                             
        if self.cleaned_data['utype'] == "F":                                                                                                                                    
            new_profile = FreeProfile(user=new_user)                                                                                                                             
        if self.cleaned_data['utype'] == "P":                                                                                                                                    
            new_profile = PremiumProfile(user=new_user)                                                                                                                             
        new_profile.save()                                                                                                                                                       
        new_base_profile.save()                                                                                                                                                  
        return new_user                

And registration phase works OK.

I’ve problem with profile edit/details pages.
My profiles filtered in ProxyProfile model and used as FormModel in ProfileForm
are not rendered (I can’t see profile specific fields are not rendered to HTML page)
Maybe there is some other way (more like Django way :)) to do this
(select and render profile model depending on user_type field which is related to User model).

Thanks in advance 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:54:14+00:00Added an answer on May 17, 2026 at 12:54 am

    Ok, finally I’ve had an idea how I can do this 🙂

    In my models.py:

    class BaseManager(models.Manager):                                                                                                                                               
        def get(self, **kwargs):                                                                                                                                                     
            self.u = kwargs['user__id__exact']                                                                                                                                       
            self.bt = BaseProfile.manager.get(user__id__exact=self.u)                                                                                                                
            if self.bt.user_type == 'F':                                                                                                                                             
                return FreeProfile.objects.get(pk=self.u)                                                                                                                            
            elif self.bt.user_type == 'I':                                                                                                                                           
                return PremiumProfile.objects.get(pk=self.u)                                                                                                                            
            else:                                                                                                                                                                    
                return None                                                                                                                                                          
    
    class BaseProfile(models.Model):                                                                                                                                                 
        objects   = BaseManager()                                                                                                                                                    
        manager   = UserManager()                                                                                                                                                    
        user      = models.OneToOneField(User, primary_key=True)                                                                                                                     
        user_type = models.CharField(max_length=1, blank=True, choices=USER_TYPES)                                                                                                   
    
    class FreeProfile(models.Model):                                                                                                                                                 
        user      = models.OneToOneField(User, primary_key=True)                                                                                                                     
        free      = models.CharField(max_length=10, blank=True) 
        ...
    
    class PremiumProfile(models.Model):                                                                                                                                                 
        user      = models.OneToOneField(User, primary_key=True)                                                                                                                     
        premium    = models.CharField(max_length=10, blank=True) 
        ...
    

    In custom manager – BaseManager I return profile object by overwriting get() method used by get_profile. I have to use UserManager named simply ‘manager’ to prevent recursive call of custom manager when assigning self.bt

    OK, this is a half way to achive what I want, now I can view different profiles attached to users using django-profiles app.

    Next, I want to use ModelForm to prepare edit form for user profiles. Users can have different profiles so I’ve applied the magic trick presented in this snippet: http://djangosnippets.org/snippets/2081/

    And now in my forms.py I have:

    class FreeForm(forms.ModelForm):                                                                                                                                                 
        class Meta:                                                                                                                                                                  
            model = FreeProfile                                                                                                                                                      
    
    
    class PremiumForm(forms.ModelForm):                                                                                                                                                 
        class Meta:                                                                                                                                                                  
            model = PremiumProfile         
    

    next, simple model forms for each profile are assembled in ProfileForm:

    class ProfileForm(ModelForm):                                                                                                                                                    
        def __init__(self, *args, **kwargs):                                                                                                                                        
        self.user = kwargs['instance'].user                                                                                                                                     
        profile_kwargs = kwargs.copy()                                                                                                                                          
        profile_kwargs['instance'] = self.user                                                                                                                                  
        self.bt = BaseProfile.manager.get(user__id__exact=self.user.id)                                                                                                         
        if self.bt.user_type == 'F':                                                                                                                                            
            self.profile_fields = FreeForm(*args, **profile_kwargs)                                                                                                             
        elif self.bt.user_type == 'P':                                                                                                                                          
            self.profile_fields = PremiumForm(*args, **profile_kwargs)                                                                                                             
        super(ProfileForm, self).__init__(*args, **kwargs)                                                                                                                      
        self.fields.update(self.profile_fields.fields)                                                                                                                          
        self.initial.update(self.profile_fields.initial) 
    
        class Meta:                                                                                                                                                                                                                                                                                                      
            model = BaseProfile     
    
        def save(self):
            ...
    

    In settings.py:

    AUTH_PROFILE_MODULE = 'main.BaseProfile'
    

    And it works like a charm but I wonder if it is the Django way to achieve support for multiple different profiles using django-profiles?
    It worries me that I have to use get() few more times before I render profile details or edit form.

    But after 4 days of struggling with Django to get this done finally I can sleep well tonight 🙂

    Cheers

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I make a distributed embedded application that will make use of several micro-controllers. The
I have a project that adds elements to an AutoCad drawing. I noticed that
I have several USB mass storage flash drives connected to a Ubuntu Linux computer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.