custom authentication in backend - Creating user at runtime without saving
Question: While searching online on how to accomplish custom authentication in Django I
came across [this](http://django.zone/blog/posts/custom-authentication-
backends-django/) and [this](https://www.djangorocks.com/tutorials/creating-a-
custom-authentication-backend/creating-a-simple-authentication-backend.html)
article. Both of these articles specified the same instructions. Currently i
have something like this. (Taken from first article)

    
    
    class Client(models.Model):
       email = models.EmailField(unique=True, max_length=100)
       password = models.CharField(max_length=128)
    

Then in another python file I have this

    
    
    from .models import Client
    
    class ClientAuthBackend(object):
    
        def authenticate(self, username=None, password=None):
            try:
                user = Client.objects.get(email=username)
                return user
    
                if password == 'master':
                    # Authentication success by returning the user
                    return user
                else:
                    # Authentication fails if None is returned
                    return None
            except Client.DoesNotExist:
                return None
    
        def get_user(self, user_id):
            try:
                return Client.objects.get(pk=user_id)
            except Client.DoesNotExist:
                return None
    

I just started using Django and have kind of skipped the model section for db
interaction since in my current project I am using RAW and custom SQL due to
certain reasons. My question is where does the

    
    
    user = Client.objects.get(email=username)
    

get its user from . Do I have to make an entry into a database ? What I want
to do is to create a user during runtime and not save to the database.I tried
doing this

    
    
    #The following creates and saves a user in the db
     u =User.objects.create_user('john', 'lennon@thebeatles.com', 'johnpassword',cust_address="my_custom_address",cust_msg="Users custom message")
    

The above returns a `Client.DoesNotExist` exception.


Answer: > My question is where does the
>  
>  
>     user = Client.objects.get(email=username)
>  
>
> get its user from

Apparently, `Client` is a `models.Model`. This means that represents a single
record of the relevant table in your database, wherever that is depending on
the relevant `settings.py` setting.

Therefore, the table representing that model can be created with the next
Django migration and lots of other useful things Django allows for.

So essentialy the above statement instructs the [Django
ORM](https://docs.djangoproject.com/ja/1.9/topics/db/) to fetch all `Client`
records from that particular table with that specific email. If no such
entries exist, none will be returned.

> I tried doing this
>  
>  
>     u =User.objects.create_user('john', 'lennon@thebeatles.com',
> 'johnpassword',cust_address="my_custom_address",cust_msg="Users custom
> message")
>  

This is where you complicate things a bit. The `create_user` method is not
part of Django ORM, but part of the [Django default auth model
manager](https://docs.djangoproject.com/es/1.9/ref/contrib/auth/#django.contrib.auth.models.UserManager.create_user)
`django.contrib.auth.models.User`. You should either provide such a method
yourself, or easier to just use the standard
[`create`](https://docs.djangoproject.com/ja/1.9/topics/db/queries/#creating-
objects) method provided with the default Django model manager.

Not saving the user model even on some cache does not make sense at all, as it
implies that the user registers each time he or she wishes to login.

Having said all those, I would strongly recommend you to read the official
Django documentation. All the above are covered, the documentation is very
comprehensive and not that long. You can then read and understand tutorials on
the wild which may or may not be correct or up to date.

Take a good read specifically on the [Customizing authentication
topic](https://docs.djangoproject.com/ja/1.9/topics/auth/customizing/), as it
provides additional methods far easier for the beginner.

