Comments on: Extending the Django User model with inheritance http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/ Code and comments on web development, Django, Python and things (un)related. Wed, 03 Apr 2013 13:15:20 +0000 hourly 1 http://wordpress.org/?v=4.3 By: Scott http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/comment-page-2/#comment-1184 Tue, 23 Oct 2012 10:04:04 +0000 http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/#comment-1184 @Plop
I think you’ll need to override get_profile() or use some other way of getting the right type of profile (e.g. get user profile if it exists else get company profile). Not sure why both profiles are being created.

]]>
By: Plop http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/comment-page-2/#comment-1175 Mon, 22 Oct 2012 22:13:21 +0000 http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/#comment-1175 Hello Scott,

I am working on a project where a User can be associated to either one profile or the other (individual || corporate). At a loss, I must confess.

This is what I have (on a file basis):

== settings.py

AUTH_PROFILE_MODULE = 'profile.Profile'
== profile/models.py

class Profile(models.Model):
 user = models.OneToOneField(User)
 class Meta:
  abstract = True

class Individual(Profile):
 # specific fields
 class Meta:
  db_table = 'individual'

class Company(Profile):
 # specific fields
  class Meta:
  db_table = 'company'

Which creates two separate tables, both pointing at the auth_user table. Fine.

== forms.py
class IndividualForm(forms.ModelForm):
 model = IndividualProfile
 fields = () # list of fields to display
== views.py (for individuals only - same for companies)
userform = UserForm(data = request.POST)
profileform = IndividualForm(data = request.POST)
if userform.is_valid() and profileform.is_valid():
 [...]
 user.save()
 profile = user.get_profile()

Now the whole thing works – up to a point. Django throws the following:

  SiteProfileNotAvailable at /.../
  Unable to load the profile model, check AUTH_PROFILE_MODULE in your project settings

The user is created. The profile is too – but in both the profile tables (individual AND company).

Any idea?

Many thanks!

]]>
By: Extending the User model with custom fields in Django http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/comment-page-2/#comment-1133 Sat, 29 Sep 2012 00:55:05 +0000 http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/#comment-1133 […] already seen a few ways to do it, but can’t decide on which one is the […]

]]>
By: Django: how to store subdomain-based authentication usernames? http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/comment-page-2/#comment-1123 Fri, 17 Aug 2012 14:15:23 +0000 http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/#comment-1123 […] extending the user model according to this. […]

]]>
By: Mark http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/comment-page-2/#comment-1122 Thu, 16 Aug 2012 14:42:31 +0000 http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/#comment-1122 This seems to work fine.

However, i cannot get the field “timezone” to appear when I am editing a CustomUser.

It will appear in the display_list fine.

But not in the edit page for a Custom User.

If i define a fieldset to include it eg:

    fieldsets = (
       (None, {'fields': ('username', 'password',)}),
       (('Personal info'), {'fields': ('first_name', 'last_name', 'email', 'timezone')}),
    )

It throws an error saying “invalid syntax” on the line where I have included “timezone”).

Any ideas?

]]>
By: Tyler http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/comment-page-2/#comment-1115 Tue, 05 Jun 2012 17:39:46 +0000 http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/#comment-1115 Thanks for the article, but I was stumped by something for a while and I thought I should share the solution.

I had made a custom user that logged in via email instead of username, and I had the login working fine for my own view that I controlled, but the admin site would not let me log in anymore. I assume it was because it was trying to use my custom authentication backend to try and log into the admin site, but that doesn’t make sense because it asks for a username and password, not a email and password like how my custom authentication backend worked.

Therefore, I had to add ‘django.contrib.auth.backends.ModelBackend’ to the end of my AUTHENTICATION_BACKENDS (under my custom backend) so that when my custom authentication failed, it would have the default django method to fall back on.

If any one has a more elegant solution to the problem I had, I’d be happy to hear it.

]]>
By: Dylan http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/comment-page-2/#comment-1113 Wed, 16 May 2012 20:44:26 +0000 http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/#comment-1113 I have a solution for those of you who have not been able to create super users with manage.py (either through syncdb or createsuperuser):

Add this near your CustomUser class in models.py:

def create_custom_user(sender, instance, created, **kwargs):
	if created:
		new_user = CustomUser(user_ptr_id=instance.id)
		new_user.__dict__.update(instance.__dict__)
		new_user.save()
post_save.connect(create_custom_user, sender=User)

Whenever a user is created it will also create an entry in the CustomUser table of the database.

]]>
By: Andy http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/comment-page-2/#comment-1106 Wed, 28 Mar 2012 05:19:05 +0000 http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/#comment-1106 So, old post, but I found it so this might be helpful for someone. Similar to #74, but if you are saving your Profile outside of the admin you can override your CustomUser’s save class. It could look something like this:

def save(self, *args, **kwargs):
    self.set_password(self.password)
    super(CustomUser, self).save(*args, **kwargs)

https://docs.djangoproject.com/en/dev/topics/db/models/#overriding-model-methods

]]>
By: Mwai http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/comment-page-2/#comment-1084 Mon, 16 Jan 2012 19:22:08 +0000 http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/#comment-1084 i get this error when i try to access my version of ‘CustomUser’ model on the admin site(i have shortened it for clarity):
no such column: app_name_publicprofile.user_ptr_id.
Here is my model:

class PublicProfile(User):
    sex = models.CharField(max_length=100,choices=settings.SEX_CHOICES,
verbose_name='sex')
    phone_num = models.IntegerField(verbose_name='phone number')
    id_num = models.IntegerField(verbose_name='id number')
    age = models.DateField(verbose_name='date of birth')
    occupation = models.CharField(max_length= 20,
verbose_name='occupation')
    picture = models.ImageField(upload_to ='user_profile_images',
verbose_name='user photo')

    objects = UserManager()

but i can add users in the admin site.

Any ideas?

]]>
By: mghs http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/comment-page-2/#comment-1071 Fri, 30 Dec 2011 09:30:31 +0000 http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/#comment-1071 Hi Scott, liked the inheritance but can anyone suggest how I can make an attribute of the child model required, if it is inheriting the User class without saving the user if the instance of the child model is not saved? eg.

class Customer(User):
organization = models.CharField(max_length=80, unique = True)
address = models.CharField(max_length=80)
.
..
objects = UserManager()

If in the admin.py, model Customer is registered, on execution, we get the user creation form, with password after saving it, we exit from the module. We are able to see that the user exists in the django Auth, even if the Customer is not yet created. How do I override the save of the User class. Also I need to create other users for the application the normal way. Please suggest

]]>