我正在制作一个巴士应用程序,在我的 models.py 中,我正在为客户和公司上课。给客户我想分配权限,比如客户可以创建帐户、修改或删除它,我想给公司类分配权限,比如公司可以添加总线详细信息、删除总线详细信息、修改它以及分配的权限给客户。所以我正在这样写我的代码。
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 完全陌生