orokusaki
orokusaki

Reputation: 57198

Django: Losing my mind and pulling my hair out with Django auth

I'm creating a SAAS as a project, and can't seem to wrap my dinosaur brain around this auth system. I started to roll my own system before I realized you could add on to the normal auth system. Is this completely wrong? Should I somehow extend the User model but still include all of my own attributes (username, password, etc)?

from django.db import models
from django.contrib.auth.models import User
from annoying.fields import AutoOneToOneField
from myproject.core.modelfields import LowerAlphanumericField
from myproject.apps.account.models import Account

class Administrator(models.Model):
    """
    Administrator for an Account (application holds multiple accounts as it is a SAAS).
    """
    account = models.ForeignKey(Account)
    user = AutoOneToOneField(User, primary_key=True)
    name = models.CharField(max_length=255)
    email = models.EmailField()
    username = LowerAlphanumericField(max_length=30)
    password = models.CharField(max_length=255)

If I visit http://127.0.0.1:8080/admin/auth/user/3/ I get an error, but the primary key for the third administrator object I created is 3 (which one would assume is the primary key for the related User object. Am I missing something. Also, do I need to create a password field, and all that junk here, or rather should I?

Upvotes: 2

Views: 718

Answers (2)

Marcus Whybrow
Marcus Whybrow

Reputation: 20008

It seams to me that you don't really need to add that much extra information. Django auth covers all the aspects you are looking for:

  • username
  • password (sha1 hash)
  • firstname
  • lastname
  • email

and Django auth also has a fairly useful permissions system:

  • superuser
  • staff
  • active

Whenever I wish to add additional information to a User object, I generally create a new model and store a reference to the User.

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    # Extra functionality

And then follow what @bste suggests, add AUTH_PROFILE_MODULE = 'accounts.UserProfile' to your settings file. This allows you to access a user profile (your extra information) with the get_profile() method on any User object.

Upvotes: 4

Stefan De Boey
Stefan De Boey

Reputation: 2394

are you already using built-in django authentication? if yes, then you can specify a model that's related to the User model in which you can store additional information about users. it's explained here: http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users

it's simple adding the following line:

AUTH_PROFILE_MODULE = 'accounts.Adminstrator'

there's no need to store the passwrd yourself, i think django does this

Upvotes: 2

Related Questions