title: Cannot assign a user in Django
body: <p>i have a simple chat application where i've customize the Authentication back end to use only the name. So far it is working and the users is logged in using Django login().</p>
  
  <p>Now i have a problem with assign a user where this Error came up and cus the rest of the app to stop:  </p>
  
  <pre><code>    ValueError: Cannot assign "&lt;SimpleLazyObject:
      &lt;django.contrib.auth.models.AnonymousUser 
      object at 0x7f7db7b71e80&gt;&gt;": "PChat.user" must be a "User" instance.
  </code></pre>
  
  <p><strong><em>View.py</em></strong></p>
  
  <pre><code>from chatbot import settings
  from django.shortcuts import render
  from django.http import HttpResponse, JsonResponse,HttpResponseRedirect
  from django.views.decorators.csrf import csrf_exempt
  from django.contrib.auth import authenticate, login
  from Pchat.models import  PChat
  from django.contrib.auth.models import User
  
  
  @csrf_exempt 
  def P_home(request):
      context= locals()
      template= 'P_home.html'
      return render(request,template,context)
  
  @csrf_exempt 
  def P_chat(request):
     context= locals()
     template= 'P_chat.html'
     return render(request,template,context)
  
  @csrf_exempt 
  def LogIIn(request):
    if request.method == "POST":
        name = request.POST.get('param1')
        user = authenticate(username=name)
        login(request, user)
    return HttpResponse("OK")
  
    @csrf_exempt 
  
    def Post(request):
       if request.method == "POST":
         msg = request.POST.get('param1')
         if request.user.is_authenticated():
            c = PChat(user=request.user, message=msg)
            c.save()
            return JsonResponse({ 'msg': msg, 'user': c.request.user })
         else:
          return HttpResponse("Account None")
  </code></pre>
  
  <p><strong><em>models.py</em></strong></p>
  
  <pre><code>  from django.db import models
    from django.db import models
    from django.contrib.auth.models import User
  
    class PChat(models.Model):
        created = models.DateTimeField(auto_now_add=True)
        user = models.ForeignKey(User)
        message = models.CharField(max_length=200)
  
        def __unicode__(self):
               return self.message
  </code></pre>
  
  <p><strong><em>auth_backend.py</em></strong></p>
  
  <pre><code>   from django.contrib.auth.backends import ModelBackend
     from django.contrib.auth.models import User
  
     class PasswordlessAuthBackend(ModelBackend):
          """Log in to Django without providing a password."""
        def authenticate(self, username=None):
           try:
               return User.objects.get(username=username)
           except User.DoesNotExist:
               return None
  
        def get_user(self, user_id):
            try:
               return User.objects.get(pk=user_id)
            except User.DoesNotExist:
               return None     
  </code></pre>