简体   繁体   中英

'WSGIRequest' object has no attribute 'CustomUser'

I want to create a patient system. User can add patients but I want to users can see only their patients. I used

patient_list = newPatients.objects.filter(current_user.id)

`but I get an error. How can I fixed it?

settings.py

    MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

views.py

@login_required
def patient_list(request):

    current_user = request.CustomUser
    patient_list = newPatients.objects.filter(current_user.id)
    query = request.GET.get('q')
    if query:
        patient_list = patient_list.filter(

            Q(first_name__icontains = query) |
            Q(last_name__icontains = query)  |
            Q(dept__icontains = query)  |
            Q(address__icontains = query)  |
            Q(phone__icontains = query)  |
            Q(notes__icontains = query))

    paginator = Paginator(patient_list, 5)  # Show 5 contacts per page

    page = request.GET.get('page')
    try:
        patients = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        patients = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        patients = paginator.page(paginator.num_pages)

    return render(request, "patients.html", {'patients':patients})

Traceback

    Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/patients/

Django Version: 3.0.3
Python Version: 3.7.0
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'users',
 'patients',
 'widget_tweaks',
 'crispy_forms',
 'ckeditor']
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')



Traceback (most recent call last):
  File "C:\Users\edeni\senior\myenv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\edeni\senior\myenv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\edeni\senior\myenv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\edeni\senior\myenv\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "C:\Users\edeni\senior\pharsys\patients\views.py", line 15, in patient_list
    current_user = request.CustomUser

Exception Type: AttributeError at /patients/
Exception Value: 'WSGIRequest' object has no attribute 'CustomUser'

In Django, you should always use request.user to get the logged-in user. If you have AUTH_MODEL = yourapp.CustomUser in your settings.py , then request.user will be an instance of your custom user model.

Secondly, your filter is incorrect:

patient_list = newPatients.objects.filter(current_user.id)

You need to say which field you are filtering on, for example:

patient_list = newPatients.objects.filter(user=current_user)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM