1. 程式人生 > >Customizing authentication in Django (定製DJango許可權功能)記憶的線索

Customizing authentication in Django (定製DJango許可權功能)記憶的線索

Extending the existing User model

There are two ways to extend the default User model without substituting your own model. If the changes you need are purely behavioral, and don’t require any change to what is stored in the database, you can create a proxy model based on User. This allows for any of the features offered by proxy models including default ordering, custom managers, or custom model methods.

★ 兩種方式擴充套件預設的 User。如果你需要增加的功能僅僅是形式上的,而不需要任何對資料庫存放的資料的改變,那麼可以使用代理模式。如此可以利用代理模式提供的任何便利,包括預設順序,定製的 Manager,定製的 model 方法等等。

If you wish to store information related to User, you can use a one-to-one relationship to a model containing the fields for additional information. This one-to-one model is often called a profile model, as it might store non-auth related information about a site user. For example you might create an Employee model:

★ 如果想儲存和 User 有關的資訊,可以使用 one-to-one relationship,關聯一個有額外資訊的模組上。這種一對一的模組通常被稱作“側寫”模組,它存放的可能是和許可權無關的User的資訊。比如下面的例子:

from django.contrib.auth.models import User

class Employee(models.Model):
    user = models.OneToOneField(User)
    department = models.CharField(max_length=100)

Assuming an existing Employee Fred Smith who has both a User and Employee model, you can access the related information using Django’s standard related model conventions:

>>> u = User.objects.get(username='fsmith')
>>> freds_department = u.employee.department

To add a profile model’s fields to the user page in the admin, define an InlineModelAdmin (for this example, we’ll use a StackedInline) in your app’s admin.py and add it to a UserAdmin class which is registered with the User class:   ★ 為 admin 裡的 user 頁面增加側寫模組裡的屬性資訊,要定義 InlineModelAdmin(例如,使用 StackedInline),並把它新增到為User類提供支援的 UserAdmin 類裡。

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User

from my_user_profile_app.models import Employee

# Define an inline admin descriptor for Employee model
# which acts a bit like a singleton
class EmployeeInline(admin.StackedInline):
    model = Employee
    can_delete = False
    verbose_name_plural = 'employee'

# Define a new User admin
class UserAdmin(UserAdmin):
    inlines = (EmployeeInline, )

# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)

These profile models are not special in any way - they are just Django models that happen to have a one-to-one link with a User model. As such, they do not get auto created when a user is created, but a django.db.models.signals.post_save could be used to create or update related models as appropriate.

★ 側寫模組沒有任何特殊之處,他們只是擁有和User 模組一對一關係的 Django 模組。因此,當 user 被建立時側寫資訊不會自動生成,使用 django.db.models.signals.post_save 方法可以建立或更新相關的模組。 

Note that using related models results in additional queries or joins to retrieve the related data, and depending on your needs substituting the User model and adding the related fields may be your better option. However existing links to the default User model within your project’s apps may justify the extra database load.

★ 要注意的是,使用相關的模組導致增加了額外的查詢和額外的檢索程式碼,並且取決於要替代 User 模組的需求的型別,增加額外的 fields 或許是更好的選擇。 

Deprecated in Django 1.5:

Deprecated since version 1.5: With the introduction of custom User models, the use of AUTH_PROFILE_MODULE to define a single profile model is no longer supported. See the Django 1.5 release notes for more information.

★ 這個功能在 v1.5 時已經廢棄了 

Prior to 1.5, a single profile model could be specified site-wide with the setting AUTH_PROFILE_MODULE with a string consisting of the following items, separated by a dot:

  1. The name of the application (case sensitive) in which the user profile model is defined (in other words, the name which was passed to manage.py startapp to create the application).
  2. The name of the model (not case sensitive) class.

For example, if the profile model was a class named UserProfile and was defined inside an application named accounts, the appropriate setting would be:

AUTH_PROFILE_MODULE = 'accounts.UserProfile'

When a user profile model has been defined and specified in this manner, each User object will have a method – get_profile() – which returns the instance of the user profile model associated with that User.

The method get_profile() does not create a profile if one does not exist.

相關推薦

Customizing authentication in Django 定製DJango許可權功能記憶線索

Extending the existing User model There are two ways to extend the default User model without substituting your own model. If the changes you need

django定製 django啟動伺服器時呼叫預設瀏覽器開啟專案首頁

(定製 django)啟動伺服器時呼叫預設瀏覽器開啟專案首頁 寫這篇 blog 的思想是:寫(程式碼)都寫了,不總結髮篇 blog 出來等著過春節嗎? 寫了對於 python 呼叫預設瀏覽器的方式也方便自己參考(不用去翻某個練習過的專案的原始碼)。 Overview

Django定製分頁功能

URL: """django_paginner URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.c

djangopython manage.py imgrate同步資料庫出錯後的解決辦法

很多情況下,因為app的models.py的檔案內容有誤,但是通過python   manage.py    check檢查不出來時,當執行python   manage.py    migrate同

Codeforces 671D. Roads in Yusland樹形DP+線段樹

pla too 不知道 ret 線上 tchar 起點 樹形 ads   調了半天居然還能是線段樹寫錯了,藥丸   這題大概是類似一個樹形DP的東西。設$dp[i]$為修完i這棵子樹的最小代價,假設當前點為$x$,但是轉移的時候我們不知道子節點到底有沒有一條越過$x$的路

840. Magic Squares In Grid 5月27日

() || 二次 邏輯 優先 pla sub 分析 flag 開頭 這是每周比賽中的第一道題,博主試了好幾次坑後才勉強做對了,第二道題寫的差不多結果去試時結果比賽已經已經結束了(尷尬),所以今天只記錄第一道題吧 題目原文 Magic Squares In Grid A

macOS 開發 - NSMicrophoneUsageDescription 10.14 許可權問題

文章目錄 升級 Mac 到 10.14 後執行專案提示: This app has crashed because it attempted to access privacy-sensitive data without a usage descrip

oracle使用者建立、授權和許可權設定關於Oracle許可權2

許可權: create session create table unlimited tablespace connect resource dba 例: #sqlplus /nolog SQL> conn / as sysdba; SQL>

iOS 一個更完善的Swift倒計時按鈕附後臺許可權申請

一個更完善的Swift倒計時按鈕(附後臺許可權申請) 如今越來越多app使用手機號碼作為使用者名稱,其中總是要涉及到驗證碼的傳送。 倒計時按鈕實現關鍵點: 當前檢視控制器銷燬後倒計時計數的再恢復 不同頁面可能使用同一個倒計時計數 app進入後臺後

3339 In Action 最短路+01揹包

簡述題意: 給出N個供電站編號從1~N,然後給出M條邊:並且給出這N個供電站的電量。每輛坦克從基地出發去攻擊供電站,並且每輛坦克只能攻擊一個供電站,求要使的破壞總電量的一半以上,求這些坦克要走的最短距離。 難度:NOIP 演算法:首先,從基地跑一次最短路,求出到所有

HDU 3926 Hand in Hand 圖的同構

題目連結 In order to get rid of Conan, Kaitou KID disguises himself as a teacher in the kindergarten. He knows kids love games and works out

SpringMVC總結之自定義註解例:許可權註解

目錄 1.前言 2.自定義一個註解 3. 定義一個相應的攔截器 4.在springMVC配置檔案中進行配置 5.在springMVC controller中使用例項 1.前言 在開發過程中,我們想給控制器的某些方法進行控制訪問許可權,或者我們需要做方法呼叫日誌記錄

Cinema in Akiba線段樹求解位置

Cinema in Akiba Time Limit: 3 Seconds Memory Limit: 65536 KB Cinema in Akiba (CIA) is a small but very popular cinema in A

樹莓派安裝系統in linux開啟ssh預設啟動

樹莓派安裝系統in linux 準備一個8g或者更大的sd卡 安裝raspberry(debain) 1.解壓系統壓縮檔案等到映象檔案 unzip 2017-03-02-raspb

NODE + JWT + Mongo簡單實現許可權管理

文章目錄 JWT簡介 應用 JWT簡介 官方是這樣介紹的: JSON Web Token (JWT) is an open standard (RFC 7519) that defines a compact and sel

ZOJ 3635——Cinema in Akiba樹狀陣列+二分

有一排座位共N個,然後有N個人,每次給一個xi,從左到右數到第xi個空位,這個空位就安排給第i個人,接著就有一系列詢問要找第i個人的位置。 因為N可以達到50000,一個個模擬肯定得T掉的。 假設當前是第i個人,用S[j]代表從左邊開始到第j個位置一共有多少個空位,對於x

SpringMVC與Shiro的最簡單整合不包含許可權管理

1.Maven 引入依賴 <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifa

bat命令更改登錄檔需管理員許可權執行

To add the key HKLM\Software\MyCo on remote computer ABC, type: REG ADD \\ABC\HKLM\Software\MyCo To add a registry entry to HKLM\Software\MyC

ZOJ 3635 Cinema in Akiba樹狀陣列 + 二分

題意: 有 n 個位置,從小到大分別編號為 1~n。然後有 n 次操作,每次取第 Ki 個沒有被取到的編號,最後詢問某次操作取出的是誰。 解題思路: 用 sum(i) 記錄 1~i 位置中,一共有幾個空座位。每次二分找到第 Ki 個是哪一個,然後將這個位置 th

[C++]LeetCode: 133 Largest Rectangle in Histogram最大矩形面積

題目: Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rec