<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.3.3" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>
<channel>
	<title>Comments on: Extending the Django User model with inheritance</title>
	<link>http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/</link>
	<description>Code and comments on web development, Django, Python and things (un)related.</description>
	<pubDate>Wed, 07 Jan 2009 04:48:56 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.3</generator>
		<item>
		<title>By: bencoder</title>
		<link>http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/#comment-605</link>
		<dc:creator>bencoder</dc:creator>
		<pubDate>Tue, 23 Dec 2008 10:51:06 +0000</pubDate>
		<guid>http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/#comment-605</guid>
		<description>The inheritance feature is great so far, but there is still a little error in the configuration.
Registration to the admin should be done this way:

[code]
from django.contrib.auth.admin import UserAdmin 
admin.site.register(CustomUser,UserAdmin)
[/code]

Then, it's also possible to set/change the password of the user</description>
		<content:encoded><![CDATA[<p>The inheritance feature is great so far, but there is still a little error in the configuration.<br />
Registration to the admin should be done this way:</p>
<p>[code]<br />
from django.contrib.auth.admin import UserAdmin<br />
admin.site.register(CustomUser,UserAdmin)<br />
[/code]</p>
<p>Then, it&#8217;s also possible to set/change the password of the user</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Thiago Alves</title>
		<link>http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/#comment-590</link>
		<dc:creator>Thiago Alves</dc:creator>
		<pubDate>Mon, 01 Dec 2008 16:25:24 +0000</pubDate>
		<guid>http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/#comment-590</guid>
		<description>Hi guys, I found a way to create my custom User model every time a auth User instance is created. I putted these lines on my models.py:

&lt;pre&gt;from django.db.models import signals

def create_profile_for_user(sender, **kwargs):
    if kwargs['created']:
        p = Profile() # 'Profile' is my custom User model
        p.__dict__.update(kwargs['instance'].__dict__)
        p.save()

signals.post_save.connect(create_profile_for_user, sender=User)&lt;/pre&gt;

I hope this helps someone else!

- Thiago</description>
		<content:encoded><![CDATA[<p>Hi guys, I found a way to create my custom User model every time a auth User instance is created. I putted these lines on my models.py:</p>
<pre>from django.db.models import signals

def create_profile_for_user(sender, **kwargs):
    if kwargs['created']:
        p = Profile() # 'Profile' is my custom User model
        p.__dict__.update(kwargs['instance'].__dict__)
        p.save()

signals.post_save.connect(create_profile_for_user, sender=User)</pre>
<p>I hope this helps someone else!</p>
<p>- Thiago</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scott</title>
		<link>http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/#comment-586</link>
		<dc:creator>Scott</dc:creator>
		<pubDate>Wed, 26 Nov 2008 10:03:55 +0000</pubDate>
		<guid>http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/#comment-586</guid>
		<description>@Sergio

If you have to add those four lines to each view, that's a fair bit of extra code, especially if you have lots of views.  You could write a custom decorator to do that and set &lt;code&gt;request.user&lt;/code&gt; or &lt;code&gt;request.custom_user&lt;/code&gt; in the decorator.

Another thing to consider is if you are making an extra call to the database when you get &lt;code&gt;request.user.custom_user&lt;/code&gt;.  The advantage to custom middleware or authentication backend is that you only need to hit the database once to get the custom user object.</description>
		<content:encoded><![CDATA[<p>@Sergio</p>
<p>If you have to add those four lines to each view, that&#8217;s a fair bit of extra code, especially if you have lots of views.  You could write a custom decorator to do that and set <code>request.user</code> or <code>request.custom_user</code> in the decorator.</p>
<p>Another thing to consider is if you are making an extra call to the database when you get <code>request.user.custom_user</code>.  The advantage to custom middleware or authentication backend is that you only need to hit the database once to get the custom user object.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sergio</title>
		<link>http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/#comment-585</link>
		<dc:creator>sergio</dc:creator>
		<pubDate>Wed, 26 Nov 2008 02:57:13 +0000</pubDate>
		<guid>http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/#comment-585</guid>
		<description>I do inheritance from User model, but i just leave the     'django.contrib.auth.middleware.AuthenticationMiddleware',
Since i can get access to my custom user trough user i do:

&lt;pre&gt;
@login_required
def my_view(request):
  try:
     cutom_user=request.user.custom_user
  except Custom_User.DoesNotExist:
     return HttpResponseRedirect(reverse('custom_client_login'))
&lt;/pre&gt;

i would like to have your feedback :)

Thanks in advance,

Sergio Hinojosa</description>
		<content:encoded><![CDATA[<p>I do inheritance from User model, but i just leave the     &#8216;django.contrib.auth.middleware.AuthenticationMiddleware&#8217;,<br />
Since i can get access to my custom user trough user i do:</p>
<pre>
@login_required
def my_view(request):
  try:
     cutom_user=request.user.custom_user
  except Custom_User.DoesNotExist:
     return HttpResponseRedirect(reverse('custom_client_login'))
</pre>
<p>i would like to have your feedback :)</p>
<p>Thanks in advance,</p>
<p>Sergio Hinojosa</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eduardo</title>
		<link>http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/#comment-574</link>
		<dc:creator>Eduardo</dc:creator>
		<pubDate>Wed, 29 Oct 2008 17:16:28 +0000</pubDate>
		<guid>http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/#comment-574</guid>
		<description>Thanks! You've saved me a lot of time! 
Nice example!!</description>
		<content:encoded><![CDATA[<p>Thanks! You&#8217;ve saved me a lot of time!<br />
Nice example!!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ravi Hasija</title>
		<link>http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/#comment-572</link>
		<dc:creator>Ravi Hasija</dc:creator>
		<pubDate>Sat, 18 Oct 2008 23:10:57 +0000</pubDate>
		<guid>http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/#comment-572</guid>
		<description>Scott, 

Thanks a million. I did look in the source and saw that authenticate method is setting the backend. Coming from Java background, I thought the child method will call super() automatically and did not know that I should call the parent directly.

It works! BEAUTIFUL! You made my day!

Thanks again for the example.</description>
		<content:encoded><![CDATA[<p>Scott, </p>
<p>Thanks a million. I did look in the source and saw that authenticate method is setting the backend. Coming from Java background, I thought the child method will call super() automatically and did not know that I should call the parent directly.</p>
<p>It works! BEAUTIFUL! You made my day!</p>
<p>Thanks again for the example.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scott</title>
		<link>http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/#comment-571</link>
		<dc:creator>Scott</dc:creator>
		<pubDate>Sat, 18 Oct 2008 22:22:32 +0000</pubDate>
		<guid>http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/#comment-571</guid>
		<description>@Ravi

I think instead of making an instance of &lt;code&gt;CustomUserModelBackend&lt;/code&gt; and calling &lt;code&gt;authenticate&lt;/code&gt; on it, you should just call the &lt;code&gt;authenticate&lt;/code&gt; function which is in &lt;code&gt;django.contrib.auth&lt;/code&gt;.

If you look in &lt;a href="http://code.djangoproject.com/browser/django/branches/releases/1.0.X/django/contrib/auth/__init__.py#L30" rel="nofollow"&gt;the source&lt;/a&gt; you can see it calls the backend's &lt;code&gt;authenticate&lt;/code&gt; method and then sets the &lt;code&gt;backend&lt;/code&gt; attribute on the user object.

It uses the backend(s) configured in your &lt;code&gt;settings.py&lt;/code&gt;, so it will be using the &lt;code&gt;CustomUserModelBackend&lt;/code&gt; anyway.

So, more like:

&lt;pre&gt;from django.contrib.auth import authenticate
...
user = authenticate(email=email, password=password)
...
login(request, user)&lt;/pre&gt;
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>@Ravi</p>
<p>I think instead of making an instance of <code>CustomUserModelBackend</code> and calling <code>authenticate</code> on it, you should just call the <code>authenticate</code> function which is in <code>django.contrib.auth</code>.</p>
<p>If you look in <a href="http://code.djangoproject.com/browser/django/branches/releases/1.0.X/django/contrib/auth/__init__.py#L30" rel="nofollow">the source</a> you can see it calls the backend&#8217;s <code>authenticate</code> method and then sets the <code>backend</code> attribute on the user object.</p>
<p>It uses the backend(s) configured in your <code>settings.py</code>, so it will be using the <code>CustomUserModelBackend</code> anyway.</p>
<p>So, more like:</p>
<pre>from django.contrib.auth import authenticate
...
user = authenticate(email=email, password=password)
...
login(request, user)</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ravi Hasija</title>
		<link>http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/#comment-570</link>
		<dc:creator>Ravi Hasija</dc:creator>
		<pubDate>Sat, 18 Oct 2008 22:03:18 +0000</pubDate>
		<guid>http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/#comment-570</guid>
		<description>I tried your example above, but when I try to login the user explicitly through login(request, user), I get the following error message: 

'CustomUser' object has no attribute 'backend'

And the code is below: 
&lt;pre&gt;def my_view(request):   
    if request.method == 'POST': # If the form has been submitted...
        form = LoginForm(request.POST) # A form bound to the POST data
        if form.is_valid():
            email = request.POST['email']
            password = request.POST['password']
            c = CustomUserModelBackend()
            user = c.authenticate(email=email, password=password)
            if user is not None:
                if user.is_active:
                    login(request, user)&lt;/pre&gt;

When I looked into the super authenticate method, it does set the backend. Not sure why it's not being set in my case.

Note: I am able to authenticate the user fine, but login fails with the given error message.
My settings.py has following entry for AUTHENTICATION_BACKENDS: 

&lt;pre&gt;AUTHENTICATION_BACKENDS = (
    'learnsite.events.AuthBackends.CustomUserModelBackend',
)&lt;/pre&gt;

Any insight will be appreciated.</description>
		<content:encoded><![CDATA[<p>I tried your example above, but when I try to login the user explicitly through login(request, user), I get the following error message: </p>
<p>&#8216;CustomUser&#8217; object has no attribute &#8216;backend&#8217;</p>
<p>And the code is below: </p>
<pre>def my_view(request):
    if request.method == 'POST': # If the form has been submitted...
        form = LoginForm(request.POST) # A form bound to the POST data
        if form.is_valid():
            email = request.POST['email']
            password = request.POST['password']
            c = CustomUserModelBackend()
            user = c.authenticate(email=email, password=password)
            if user is not None:
                if user.is_active:
                    login(request, user)</pre>
<p>When I looked into the super authenticate method, it does set the backend. Not sure why it&#8217;s not being set in my case.</p>
<p>Note: I am able to authenticate the user fine, but login fails with the given error message.<br />
My settings.py has following entry for AUTHENTICATION_BACKENDS: </p>
<pre>AUTHENTICATION_BACKENDS = (
    'learnsite.events.AuthBackends.CustomUserModelBackend',
)</pre>
<p>Any insight will be appreciated.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steinn</title>
		<link>http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/#comment-569</link>
		<dc:creator>Steinn</dc:creator>
		<pubDate>Wed, 15 Oct 2008 11:59:52 +0000</pubDate>
		<guid>http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/#comment-569</guid>
		<description>Thanks Scott, I'll take a look at these links.

Each customer is a company, and can have several users under it, so unfortunately I can't go with the route of having the CustomUser be the Customer.

One thing I realized yesterday after posting my question here, was that when I go into the administration I can go and create a CustomUser as part of my application, and that will of course create the entries in both tables (and allows me to choose which Customer the new user belongs to). Since all the users will be created this way, I think I can manage without writing a new manager :-)

Best,
Steinn</description>
		<content:encoded><![CDATA[<p>Thanks Scott, I&#8217;ll take a look at these links.</p>
<p>Each customer is a company, and can have several users under it, so unfortunately I can&#8217;t go with the route of having the CustomUser be the Customer.</p>
<p>One thing I realized yesterday after posting my question here, was that when I go into the administration I can go and create a CustomUser as part of my application, and that will of course create the entries in both tables (and allows me to choose which Customer the new user belongs to). Since all the users will be created this way, I think I can manage without writing a new manager :-)</p>
<p>Best,<br />
Steinn</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scott</title>
		<link>http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/#comment-568</link>
		<dc:creator>Scott</dc:creator>
		<pubDate>Tue, 14 Oct 2008 23:46:13 +0000</pubDate>
		<guid>http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/#comment-568</guid>
		<description>@Steinn

You are correct that creating a &lt;code&gt;User&lt;/code&gt; in admin will not cause a &lt;code&gt;CustomUser&lt;/code&gt; to be created.  You need to make it so &lt;code&gt;CustomUser&lt;/code&gt;s are created in admin.  See comment #8 above for an idea of how to do that.

Does &lt;code&gt;Customer&lt;/code&gt; need to be a separate model from &lt;code&gt;CustomUser&lt;/code&gt;?  If not, you could use &lt;code&gt;Customer&lt;/code&gt; as your custom user class -- that's what I'm doing in my current project.

If they do need to be separate, you could make the &lt;code&gt;customer&lt;/code&gt; property of &lt;code&gt;CustomUser&lt;/code&gt; optional so a &lt;code&gt;CustomUser&lt;/code&gt; can be created first then a &lt;code&gt;Customer&lt;/code&gt; can be created and assigned.  That's a bit messy, though.

An alternative is to write your own manager for &lt;code&gt;CustomUser&lt;/code&gt; and give it a &lt;code&gt;create_user&lt;/code&gt; method that does the right thing.  You can look at the source in &lt;code&gt;&lt;a href="http://code.djangoproject.com/browser/django/branches/releases/1.0.X/django/contrib/auth/models.py#L105" rel="nofollow"&gt;django/contrib/auth/models.py&lt;/a&gt;&lt;/code&gt; to see what the default method does (not much), copy and customise.

If there is always one &lt;code&gt;Customer&lt;/code&gt; for each &lt;code&gt;CustomUser&lt;/code&gt;, it seems like maybe they should be the same model, so that would be the first thing to consider.</description>
		<content:encoded><![CDATA[<p>@Steinn</p>
<p>You are correct that creating a <code>User</code> in admin will not cause a <code>CustomUser</code> to be created.  You need to make it so <code>CustomUser</code>s are created in admin.  See comment #8 above for an idea of how to do that.</p>
<p>Does <code>Customer</code> need to be a separate model from <code>CustomUser</code>?  If not, you could use <code>Customer</code> as your custom user class &#8212; that&#8217;s what I&#8217;m doing in my current project.</p>
<p>If they do need to be separate, you could make the <code>customer</code> property of <code>CustomUser</code> optional so a <code>CustomUser</code> can be created first then a <code>Customer</code> can be created and assigned.  That&#8217;s a bit messy, though.</p>
<p>An alternative is to write your own manager for <code>CustomUser</code> and give it a <code>create_user</code> method that does the right thing.  You can look at the source in <code><a href="http://code.djangoproject.com/browser/django/branches/releases/1.0.X/django/contrib/auth/models.py#L105" rel="nofollow">django/contrib/auth/models.py</a></code> to see what the default method does (not much), copy and customise.</p>
<p>If there is always one <code>Customer</code> for each <code>CustomUser</code>, it seems like maybe they should be the same model, so that would be the first thing to consider.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.167 seconds -->
