gpt4 book ai didi

python - 为django中的组分配权限

转载 作者:行者123 更新时间:2023-11-30 23:31:28 25 4
gpt4 key购买 nike

我正在制作一个公交车应用程序,在我的 models.py 中,我正在为客户和公司提供类(class)。对于客户,我想分配权限,例如客户可以创建帐户、修改或删除帐户,对于公司类,我想分配权限,例如公司可以添加公交车详细信息、删除公交车详细信息、修改它以及分配的权限给客户。所以我像这样编写代码。

from django.contrib.auth.models import User, Group, Permission
fom django.contrib.contenttypes.models import ContentType

companyRep = Group(name ='Company Permission')
companyRep.save()
customerPerm = Group(name = 'Customer Permission')
customerPerm.save()

userPerm = ContentType.objects.get(app_label='busapp', model = 'user')
can_add_bus = Permission(name = 'AddBus', codename = 'can_add_bus', content_type = 'userPerm')
can_add_bus.save()
can_delete_bus = Permission(name = 'deleteBus', codename = 'can_delete_bus', content_type = 'userPerm')
can_delete_bus.save()
can_create_profile = Permission(name = 'createProfile', codename = 'can_create_profile', content_type = 'userPerm')
can_create_profile.save()
can_delete_profile = Permission(name = 'deleteProfile', codename = 'can_delete_profile', content_type = 'userPerm')
can_delete_profile.save()
can_view_profile = Permission(name ='viewProfile', codename = 'can_view_profile', content_type = 'userPerm')
can_view_profile.save()

companyRep.permissions=[can_view_profile,can_delete_profile,can_create_profile,can_delete_bus, can_add_bus]
customerPerm.permissions = [can_view_profile,can_delete_profile,can_create_profile]

class Company(models.Model):
#username associated to authenticate company
user = models.OneToOneField(User)
#name of the company
name = models.CharField(max_length=20)
#account identifier to carry our transaction handling
account_number = models.CharField(max_length=10)
#the phone number of the associated manager
manager_phone = models.IntegerField()

def __unicode__(self):
return self.name
class Customer(models.Model):
#username associated with the customer
user = models.OneToOneField(User)
#first name
fname = models.CharField(max_length=20)
#last name
lname = models.CharField(max_length=20)
#phone number
phone_number = models.IntegerField()
#bank account
account_number = models.CharField(max_length=10)
#address
address = models.TextField()
#date of birth
dob = models.DateField()
#gender
gender = models.CharField(max_length=6)

我收到错误 IntegrityError:列名称不唯一。任何人都可以帮我为这些组分配权限吗?我对 Django 完全陌生

最佳答案

models.py 应该只包含模型类:

from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.db import models

class Company(models.Model):
#username associated to authenticate company
user = models.OneToOneField(User)
#name of the company
name = models.CharField(max_length=20)
#account identifier to carry our transaction handling
account_number = models.CharField(max_length=10)
#the phone number of the associated manager
manager_phone = models.IntegerField()

def __unicode__(self):
return self.name

class Customer(models.Model):
#username associated with the customer
user = models.OneToOneField(User)
#first name
fname = models.CharField(max_length=20)
#last name
lname = models.CharField(max_length=20)
#phone number
phone_number = models.IntegerField()
#bank account
account_number = models.CharField(max_length=10)
#address
address = models.TextField()
#date of birth
dob = models.DateField()
#gender
gender = models.CharField(max_length=6)

其余部分可以在 django shell 中运行:

$ python ./manage.py shell
Python 2.7.2 (default, Oct 11 2012, 20:14:37)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from busApp.models import Company, Customer
>>> from django.contrib.auth.models import User, Group, Permission, ContentType
>>> companyRep = Group(name = 'Company Permission')
>>> companyRep.save()
>>> companyRep_contentType = ContentType()
>>> companyRep_contentType.name = 'userPerm'
>>> companyRep_contentType.save()
>>> can_add_bus = Permission(name='AddBus', codename='can_add_bus', content_type=companyRep_contentType)
>>> can_add_bus.save()
>>> companyRep.permissions=[can_add_bus]
>>> companyRep.save()
>>> companyRep.permissions.all()
[<Permission: | userPerm | AddBus>]
>>> from django.contrib.auth.models import User
>>> company_user = User()
>>> from busApp.models import Company
>>> company_user.save()
>>> company_user.groups.all()
[]
>>> company_user.groups.add(companyRep)
>>> company_user.save()
>>> company_user.groups.all()
[<Group: Company Permission>]
>>> newCompany
>>> newCompany = Company()
>>> newCompany.user = company_user
>>> newCompany.save()
>>> newCompany.user
>>> newCompany.user.get_group_permissions()
set([u'.can_add_bus'])
>>>

为了简洁起见,我只创建了您的部分权限,但希望您能明白。这只需要运行一次,并且可能在你的生产服务器(django 系统最终驻留的地方)上再次运行。然后可以在 views.py 和模板 useful blog post 中查询 CustomerCompany 模型的权限。对这个。

这对您当前的错误消息有帮助吗?

关于python - 为django中的组分配权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19892641/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com