2015-11-06 55 views
0

在我的Django項目我有用戶,每個用戶可以擁有不同的權限(有的一個具有隻讀權限,但其他人可能會讀取和寫入權限),我想補充permissions領域的Django auth_user_groups表是這樣的:如何將字段添加到django中的`auth_user_groups`表中?

enter image description here 這裏是我的models.py:

from django.db.models import * 
from django.contrib.auth.models import User, Group 
from django.db.models.signals import post_save 


class MyUser(Model): 
    user = OneToOneField(User, primary_key=True) 
    Volume = IntegerField(default=5*1024*1024) 


def create_profile(sender, instance, created, **kwargs): 
    if created: 
     profile, created = MyUser.objects.get_or_create(user=instance) 

post_save.connect(create_profile, sender=User, dispatch_uid="create_profile") 

是有可能做到這一點?

回答

1

我會建議您創建一個與auth_user_groups的一對一關係的附加表來解決您的問題,而不是擺弄那個。這與文檔中建議的the one類似。另外,爲了提供您自己的模型,您需要做很多工作。

編輯:繼續第一條評論:由於User模型的相應多對多字段不使用明確的intermediate model(實際上多對多字段屬於PermissionsMixin,導出AbstractUser,導出User)。

一個one-to-one字段本質上是一個多對一的外鍵,unique=True。因此,您可以創建兩個外鍵,一個用於用戶,另一個用於組unique_together,然後使用這些外鍵。

+0

謝謝。但我怎樣才能創建一個'OneToOneField'到'auth_user_groups'表?我的意思是我可以輸入的名字是什麼? (比如'from django.contrib.auth.models import User,Group'這是什麼名字?我試過'from django.contrib.auth.models import UserGroups'但它不正確) – shotgunner

+0

歡迎使用@shotgunner,我看到你的點,更新。 – Wtower

0

Django向用戶組和個人用戶提供權限,您可以創建多個組併爲這些組分配權限。並將組分配給每個用戶。因此,與特定組相關的用戶將擁有相同的權限集。

這裏指的更多的https://docs.djangoproject.com/en/1.8/topics/auth/default/#permissions-and-authorization

+1

謝謝。我的項目中的所有組都具有相同的權限(讀,寫|寫)。但他們中的用戶可能已經讀取或讀取權限。這不能用你說的處理。 – shotgunner

相關問題