Search code examples
pythondjangodjango-modelsdjango-viewscustom-model-binder

make django look for data in my custom model


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 ??


Solution

  • 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.