User model extension in django

http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/

When it comes to extending the User model, the above article lists two methods: the old method (ForeignKey) and the new method (user model with inheritance). But at the same time, this article dates from August 2008.

I am using the Django development version.

Would you recommend extending a Django user model with inheritance or using ForeignKey?

I read in several posts that the django.contrib.auth.models.User extension is not recommended, so I will not look at that.

+3
source share
3 answers

AFAIK, - - AUTH_PROFILE_MODEL, Django User.

. Django Doc

+1

. AUTH_PROFILE_MODEL. "Django in Depth". http://www.youtube.com/watch?v=t_ziKY1ayCo&feature=related 1 : 37 .

  • , , BngGangOfFour.
  • Model, UserProfile , .

BngGangOfFour/models.py

from django.contrib.auth.models import User

class UserProfile(models.Model):
    user = models.OneToOneField(User) #notice it must contain a 1 to 1 field with the auth user.
    last_ip_address = models.CharField(max_length=20, default="")
  • settings.py, .

settings.py

....
AUTH_PROFILE_MODULE = 'BngGangOfFour.UserProfile' #not case sensitive.
....
  • .

BngGangOfFour/views.py

....
def index(request):
    if request.user.get_profile().last_ip_address = "127.0.0.1":
        print("why hello me!")    
    return render_to_response('index.html', locals(), context_instance=RequestContext(request))
  • .
+1

The only time you can completely leave with the extension Userthrough inheritance is if you create a backup server that will return an instance of the corresponding model.

0
source

All Articles