1

I'm making a bus app and in my models.py i'm having classes customer and company. To customer i want to assign permissions like customer can create account, modify it or delete it and to the company class i want to assign permissions like a company can add a bus detail, delete the bus detail, modify it as well as the permissions assigned to customer. So i'm writing my code like this.

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)

I'm getting an error IntegrityError: coloumn name is not unique. Can any one please help me with assigning permissions to these groups. am totally new to django

4

1 に答える 1