简体   繁体   中英

How to Create Custom Permission and Group model in django?

i am trying to create my Custom Permission and Group model with following code but when i try to migrate i got error "django.db.utils.ProgrammingError: relation "auth_permission" already exists"

class Role(models.Model):
    def __unicode__(self):
        return self.name

    # slug          = models.CharField(max_length=50, primary_key=True)
    name = models.CharField(max_length=50, blank=True)

    class Meta:
        db_table = 'auth_group'


# ROLE_CHOICES = (('superuser', 'Super User'),('user', 'User'))

class Permission(models.Model):
    def __unicode__(self):
        return self.name

    codename = models.CharField(max_length=50, blank=False)
    name = models.CharField(max_length=50, blank=False)

    class Meta:
        db_table = 'auth_permission'

---------------------------Settings.py----------------------------

INSTALLED_APPS = (
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'rest_framework.authtoken',
    'django_filters',
    'sparkAuth',
    # Uncomment the next line to enable the admin:
    # 'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
)
class Permission(models.Model):
    def __unicode__(self):
        return self.name

    codename = models.CharField(max_length=50, blank=False)
    name = models.CharField(max_length=50, blank=False)

    class Meta:
        db_table = 'auth_permission'

You expect your table to be named auth_permission which is already being used by the Permission model in the django.contrib.auth app. This is why the error says:

django.db.utils.ProgrammingError: relation "auth_permission" already exists

Solution:

  • Choose a different table name
  • Do not specify the table name, then it will be in the form of <app>_<modelclass>
  • Another very bad idea would be to remove the django.contrib.auth from INSTALLED_APPS in settings.py but then you risk breaking a lot of stuff and you probably really don't want that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM