import json
from django.contrib.auth import get_user_model
from dajaxice.decorators import dajaxice_register


@dajaxice_register(method="GET")
def getuser(request, query):
    User = get_user_model()

    qs = User.objects.filter(username__icontains=query) |\
        User.objects.filter(first_name__icontains=query) |\
        User.objects.filter(last_name__icontains=query)

    result = [
        {'username': u.username,
         'label': '%s (%s)' % (u.get_full_name(), u.username)}
        for u in qs.distinct()]
    return json.dumps(result)


import json
from django.contrib.auth import get_user_model
from dajaxice.decorators import dajaxice_register


@dajaxice_register(method="GET")
def getuser(request, query):
    User = get_user_model()

    qs = User.objects.filter(username__icontains=query) |\
        User.objects.filter(first_name__icontains=query) |\
        User.objects.filter(last_name__icontains=query)
    qs = qs.exclude(id=request.user.id).exclude(id=-1)

    result = [
        {'username': u.username,
         'label': '%s (%s)' % (u.get_full_name(), u.username)}
        for u in qs.distinct()]
    return json.dumps(result)


Exclude self and anonymous from message recipients in autocomplete.
Exclude self and anonymous from message recipients in autocomplete.

  import json
  from django.contrib.auth import get_user_model
  from dajaxice.decorators import dajaxice_register
  
  
  @dajaxice_register(method="GET")
  def getuser(request, query):
      User = get_user_model()
  
      qs = User.objects.filter(username__icontains=query) |\
          User.objects.filter(first_name__icontains=query) |\
          User.objects.filter(last_name__icontains=query)
+     qs = qs.exclude(id=request.user.id).exclude(id=-1)
  
      result = [
          {'username': u.username,
           'label': '%s (%s)' % (u.get_full_name(), u.username)}
          for u in qs.distinct()]
      return json.dumps(result)
  
  
Exclude self and anonymous from message recipients in autocomplete.
## Code Before:
import json
from django.contrib.auth import get_user_model
from dajaxice.decorators import dajaxice_register


@dajaxice_register(method="GET")
def getuser(request, query):
    User = get_user_model()

    qs = User.objects.filter(username__icontains=query) |\
        User.objects.filter(first_name__icontains=query) |\
        User.objects.filter(last_name__icontains=query)

    result = [
        {'username': u.username,
         'label': '%s (%s)' % (u.get_full_name(), u.username)}
        for u in qs.distinct()]
    return json.dumps(result)


## Instruction:
Exclude self and anonymous from message recipients in autocomplete.
## Code After:
import json
from django.contrib.auth import get_user_model
from dajaxice.decorators import dajaxice_register


@dajaxice_register(method="GET")
def getuser(request, query):
    User = get_user_model()

    qs = User.objects.filter(username__icontains=query) |\
        User.objects.filter(first_name__icontains=query) |\
        User.objects.filter(last_name__icontains=query)
    qs = qs.exclude(id=request.user.id).exclude(id=-1)

    result = [
        {'username': u.username,
         'label': '%s (%s)' % (u.get_full_name(), u.username)}
        for u in qs.distinct()]
    return json.dumps(result)

