from django.db import models from django.contrib.auth.models import User class Person(User): class Meta: <!--設置proxy=True,標記該模型不用映射到數據庫中,只是一個代理--> proxy = True <!--定義一個類方法,cls就是當前的類對象,即user--> @classmethod def get_blacklist(cls): return cls.objects.filter(is_active=False)
from .models import Person from django.http import HttpResponse from django.contrib.auth.models import User from django.shortcuts import render def index(request): users = Person.get_blacklist() if users: print(type(users)) for user in users: print(user.username) context = { 'users': users } return render(request, 'proxy.html',context=context) else: context = { 'users': '沒有查找到誒!' } return render(request, 'proxy.html', context=context)
<ul> {% for user in users %} <li>未激活的帳號:{{ user.username }}</li> {% endfor %} </ul>
==在瀏覽器中,就會顯示當前數據庫中is_active爲False的用戶。==html