0

i am new to django and concept i am trying is very simple. I created the custom model(i dont want to extend any pre-defined model). And using this code i tried to authenticate my login :

Models.py : (i have given just sample data here. This is not the real data i use. And my client dosent want me to use any builtin models like AbstractBaseUser etc.,)

from django.db import models
#from django.contrib.auth.models import User

class logindata(models.Model):
    fname= models.CharField(max_length=30)
    lname = models.CharField(max_length=30)
    uname = models.CharField(max_length=30)
    password = models.CharField(max_length = 30)

Views.py

def auth_view(request):
username = request.POST.get('username','')
password = request.POST.get('password','')
user =auth.authenticate(uname=username, password=password)
if user is not None:    
    auth.login(request, user)
    return HttpResponseRedirect('/accounts/loggedin',{'user':user})
else:
    return HttpResponseRedirect('/accounts/invalid')

def loggedin(request):
    return render_to_response('loggedin.html',
                              {'name':request.user.uname})

My question is very simple. i want authenticate to look at my custom model(logindata) instead of the default one. how do i do that ??

seshan
  • 99
  • 2
  • 11
  • 1
    Why your client dosent want you to use any builtin models like AbstractBaseUser? – Leonardo.Z Oct 09 '13 at 06:09
  • i tried talking to the client i dont know why ... he dosent want it.. he told anything you design design it custom... and moreover this is first project on django so not sure how to proceed. – seshan Oct 09 '13 at 06:24
  • @S.Ramaseshan then why use a framework at the first place? Write a custom framework from scratch. If you're using a framework, that implicitly means you're reusing codes. The client is defying his/her own decision of using Django. – Bibhas Debnath Oct 09 '13 at 06:29

2 Answers2

1

Try defining you custom model using the settings AUTH_USER_MODEL

I didn't test it myself, and perhaps it will require you to subclass the django BaseUser but you should try.


  • After you update that it is ok to use Django base model,I can say that this approach will work for sure.
YardenST
  • 5,119
  • 2
  • 33
  • 54
1

If you want to create a custom user model, Django provides an django.contrib.auth.models.AbstractBaseUser model that you can extend and use. Django documentation has a whole section dedicated to that, take a look.

If you’re entirely happy with Django’s User model and you just want to add some additional profile information, you can simply subclass django.contrib.auth.models.AbstractUser and add your custom profile fields. This class provides the full implementation of the default User as an abstract model.

You can go through the code at github to see what to extend.

Bibhas Debnath
  • 14,559
  • 17
  • 68
  • 96
  • forget the login... say i want to validate something else that is defined custom... how exactly would i proceed with that... – seshan Oct 09 '13 at 06:52
  • @S.Ramaseshan If it's a model field, you could validate using a form. – Bibhas Debnath Oct 09 '13 at 06:53
  • @S.Ramaseshan to `authenticate()` to use your model, your model must be identical to `AbstractBaseUser`, because it needs the properties and methods defined in that abstract model. You can not just skip it. – Bibhas Debnath Oct 09 '13 at 06:59
  • @S.Ramaseshan Updated the answer depending on your updated question. – Bibhas Debnath Oct 09 '13 at 07:07
  • **Update** The client told OK for using builtin models... now might start up with AbstractBaseUser and stuff – seshan Oct 09 '13 at 07:21