1

我正在制作一个巴士应用程序,在我的 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 完全陌生

4

1 回答 1

3

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 系统最终将驻留在该服务器上)。然后可以在和 模板中查询Customer和模型的权限,有关此的有用博客文章Companyviews.py

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

于 2013-11-10T19:01:27.207 回答