Skip to content

Instantly share code, notes, and snippets.

@yarbelk
Created May 26, 2012 07:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yarbelk/2792846 to your computer and use it in GitHub Desktop.
Save yarbelk/2792846 to your computer and use it in GitHub Desktop.
Example to illustrate a problem I'm having with multi-table inheritance for user profiles in django and Userena
# This is the form to sign up a Spam Type Person
from django import forms
from userena.forms import SignupFormTos
from . models import CommonProfile, SpamProfile
class SpamSignupForm(SignupFormTos):
"""signup a Spam Person"""
common_field = forms.CharField(label='what is your quest')
spam_field = forms.CharField(label='what kind of spam are you')
def clean(self):
cleaned_data = super(SpamSignupForm,self).clean()
#do stuf, same idea for clean_<field>
return cleaned_data
def save(self):
"""Save the SpamProfile"""
user = super(SpamSignupForm,self).save()
common_profile = user.get_profile()
spam_profile = SpamProfile(commonprofile_ptr=common_profile)
spam_profile.spam_field = self.cleaned_data['spam_field']
spam_profile.save()
return spam_profile
# This is an example of how i've attempted to get
# multiple user profiles working with Userena
from django.contrib.auth.models import User
from django.db import models
from userena.models import UserenaLanguageBaseProfile
class CommonProfile(UserenaLanguageBaseProfile):
"""Common fields for 2 user profiles: Spam and Eggs"""
user = models.OneToOneField(User)
common_field = models.CharField(max_length=100)
@property
def is_spam(self):
"""Find out if this is a Spam user"""
try:
self.spamprofile
return True
except SpamProfile.DoesNotExist:
return False
def get_real_type(self):
"""return the real model"""
if self.is_spam:
return self.spamprofile
else:
return self.eggsprofile
class SpamProfile(CommonProfile):
spam_field = models.CharField(max_length=20)
class EggsField(CommonProfile):
eggs_field = models.SmallIntegerField()
@yarbelk
Copy link
Author

yarbelk commented May 26, 2012

Example code for a problem I'm having using Userena and multi-table inheritance to have multiple profile types. When I do this, no data gets populated in the SpamProfile table, and all users are of type CommonProfile, which should not be aloud.

@LeonardoGentile
Copy link

I've seen you asked this on SO: http://stackoverflow.com/q/10778980/1191416
Could you please tell us if you have found a solution? If positive it would be very kind of you to share it.
I'm also struggling with it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment