Help with customizing Django authentication

61 views
Skip to first unread message

Carlos Ribas

unread,
May 27, 2015, 11:54:59 AM5/27/15
to django...@googlegroups.com
Hello All,

I am currently extending the existing User model to store additional information. So, basically I have:

# models.py
class Person(models.Model):
    user = models.OneToOneField(User, verbose_name=_('User'))
    zipcode = models.CharField(_('Zip Code'), max_length=9, blank=True, null=True)
    street = models.CharField(_('Address'), max_length=255, blank=True, null=True)
    ...

#admin.py
class PersonAdmin(admin.StackedInline):
    model = Person
    ...

class UserAdmin(UserAdmin):
    inlines = (PersonAdmin, )
    ...

admin.site.unregister(User)
admin.site.register(User, UserAdmin)   


The problem is that my system should be able to register a new person, but this person may not need an account on the system (I just need to have his/her information). Right now, I can not create a person without an account. Besides that, first_name and last_name fields are not in the person class. 

I am wondering what is the best solution for my case. Probably, I will need to move the first_name and last_name fields to the Person class, and to do that, I will have to create custom users, is that right? 

If that is the case, may I just copy and paste the lines shown here (https://github.com/django/django/blob/master/django/contrib/auth/models.py) and remove the lines about first_name and last_name? 

Well, any help will be appreciated.  

Vishnuprabha Sridharan

unread,
May 29, 2015, 7:33:50 AM5/29/15
to django...@googlegroups.com
hi ,

Try to use the django administration to create the user authentication with admin.py,views.py and forms.py

try by using this link:


Confidentiality Notice: The above information contained in this email is intended for the confidential use of the above-named recipient(s). If a reader of this message is not the intended recipient or person responsible for delivering it to the intended recipient, you are hereby notified that you have received this communication in error, and that any review, dissemination, distribution, or copying of this communication is strictly prohibited. If you have received this in error, please notify the sender immediately and destroy this message.**

Vishnuprabha Sridharan

unread,
May 29, 2015, 7:33:50 AM5/29/15
to django...@googlegroups.com
Hi,

Try this views.py 

from django.contrib.auth import authenticate
user = authenticate(username='', password='secret')
if user is not None:
    # the password verified for the user
    if user.is_active:
        print("User is valid, active and authenticated")
    else:
        print("The password is valid, but the account has been disabled!")
else:
    # the authentication system was unable to verify the username and password
    print("The username and password were incorrect.")

On Wednesday, May 27, 2015 at 9:24:59 PM UTC+5:30, Carlos Ribas wrote:

Carlos Ribas

unread,
May 29, 2015, 5:19:00 PM5/29/15
to django...@googlegroups.com
Hello,

I have to confess that I did not understand your suggestion. How this will help me to reverse the logic of my system? I mean, instead of User with or without a profile (the Person class, in my case), I want a Person with or without a User.

Thanks anyway

Carl Meyer

unread,
May 29, 2015, 5:53:25 PM5/29/15
to django...@googlegroups.com
Hello Carlos,
Yes, the best solution for your case (and for all Django projects) is to
use a custom User model.

In order to have every User linked to a Person, but not all Persons
linked to Users, you need to place the OneToOneField on the User model
pointing to Person, rather than the other way around. And in order to do
that, you need to have control over the User model.

(You _could_ sort of do it without a custom User model by having a
ManyToMany relationship between User and Person, but that allows a wide
variety of situations you don't want to allow.)

> If that is the case, may I just copy and paste the lines shown here
> (https://github.com/django/django/blob/master/django/contrib/auth/models.py
> <https://github.com/django/django/blob/master/django/contrib/auth/models.py>)
> and remove the lines about first_name and last_name?

No, you should follow the documentation on custom User models.

You'll want to inherit from AbstractBaseUser instead of AbstractUser, so
as to not have first_name and last_name fields. You'll need to add the
PermissionsMixin and probably a few other fields (including a username
field). The docs should explain what you need to know.

Carl

signature.asc

Carlos Ribas

unread,
Jun 2, 2015, 5:10:05 PM6/2/15
to django...@googlegroups.com
Hello Carl, 

I really appreciate your comments and I agree with you. I'll put here soon the code I'm writing, maybe this can be helpful for others too.

Thanks!
Reply all
Reply to author
Forward
0 new messages