首页 > 代码库 > django 1.7+ default_permissions

django 1.7+ default_permissions

由于做Caption要做权限设计。在核心类的设计的时候需要做好权限的基础设计。django 1.7+以后 django.db.modes新增特性 default_permissions,官方文档语焉不详。

决定自己探索下,不想一一分析代码,遂引入bug,直接观察核心线路。

引入bug方法:

class baseModel(models.Model):    #....    class Meta:        default_permissions=‘add‘#should be (‘add‘,‘change‘,...)

错误如下:

Traceback (most recent call last):  File "C:\Users\tommy.yu\Desktop\Captain\Captain-master\codes\Captain\manage.py", line 10, in <module>    execute_from_command_line(sys.argv)  File "C:\Python27\lib\site-packages\django-1.7.2-py2.7.egg\django\core\management\__init__.py", line 385, in execute_from_command_line    utility.execute()  File "C:\Python27\lib\site-packages\django-1.7.2-py2.7.egg\django\core\management\__init__.py", line 377, in execute    self.fetch_command(subcommand).run_from_argv(self.argv)  File "C:\Python27\lib\site-packages\django-1.7.2-py2.7.egg\django\core\management\base.py", line 288, in run_from_argv    self.execute(*args, **options.__dict__)  File "C:\Python27\lib\site-packages\django-1.7.2-py2.7.egg\django\core\management\base.py", line 338, in execute    output = self.handle(*args, **options)  File "C:\Python27\lib\site-packages\django-1.7.2-py2.7.egg\django\core\management\commands\migrate.py", line 165, in handle    emit_post_migrate_signal(created_models, self.verbosity, self.interactive, connection.alias)  File "C:\Python27\lib\site-packages\django-1.7.2-py2.7.egg\django\core\management\sql.py", line 268, in emit_post_migrate_signal    using=db)  File "C:\Python27\lib\site-packages\django-1.7.2-py2.7.egg\django\dispatch\dispatcher.py", line 198, in send    response = receiver(signal=self, sender=sender, **named)  File "C:\Python27\lib\site-packages\django-1.7.2-py2.7.egg\django\contrib\auth\management\__init__.py", line 114, in create_permissions    Permission.objects.using(using).bulk_create(perms)  File "C:\Python27\lib\site-packages\django-1.7.2-py2.7.egg\django\db\models\query.py", line 409, in bulk_create    self._batched_insert(objs_without_pk, fields, batch_size)  File "C:\Python27\lib\site-packages\django-1.7.2-py2.7.egg\django\db\models\query.py", line 938, in _batched_insert    using=self.db)  File "C:\Python27\lib\site-packages\django-1.7.2-py2.7.egg\django\db\models\manager.py", line 92, in manager_method    return getattr(self.get_queryset(), name)(*args, **kwargs)  File "C:\Python27\lib\site-packages\django-1.7.2-py2.7.egg\django\db\models\query.py", line 921, in _insert    return query.get_compiler(using=using).execute_sql(return_id)  File "C:\Python27\lib\site-packages\django-1.7.2-py2.7.egg\django\db\models\sql\compiler.py", line 920, in execute_sql    cursor.execute(sql, params)  File "C:\Python27\lib\site-packages\django-1.7.2-py2.7.egg\django\db\backends\utils.py", line 81, in execute    return super(CursorDebugWrapper, self).execute(sql, params)  File "C:\Python27\lib\site-packages\django-1.7.2-py2.7.egg\django\db\backends\utils.py", line 65, in execute    return self.cursor.execute(sql, params)  File "C:\Python27\lib\site-packages\django-1.7.2-py2.7.egg\django\db\utils.py", line 94, in __exit__    six.reraise(dj_exc_type, dj_exc_value, traceback)  File "C:\Python27\lib\site-packages\django-1.7.2-py2.7.egg\django\db\backends\utils.py", line 65, in execute    return self.cursor.execute(sql, params)  File "C:\Python27\lib\site-packages\django-1.7.2-py2.7.egg\django\db\backends\sqlite3\base.py", line 486, in execute    return Database.Cursor.execute(self, query, params)django.db.utils.IntegrityError: columns content_type_id, codename are not unique

  

直接在最终的文件C:\Python27\lib\site-packages\django-1.7.2-py2.7.egg\django\db\backends\sqlite3\base.py里面加入print xxx之类的语句。如下:

    def execute(self, query, params=None):        if params is None:            return Database.Cursor.execute(self, query)        query = self.convert_query(query)        print query,params#在这里添加了print        return Database.Cursor.execute(self, query, params)

再次migrate,并观察sql:

SELECT "django_migrations"."app", "django_migrations"."name" FROM "django_migrations" ()Operations to perform:  Synchronize unmigrated apps: mptt  Apply all migrations: admin, Core, contenttypes, auth, sessionsSynchronizing apps without migrations:  Creating tables...  Installing custom SQL...  Installing indexes...Running migrations:  No migrations to apply.SELECT "django_content_type"."id", "django_content_type"."name", "django_content_type"."app_label", "django_content_type"."model" FROM "django_content_type" WHERE ("django_content_type"."model" = ? AND "django_content_type"."app_label" = ?) LIMIT 21 (‘logentry‘, ‘admin‘)SELECT "auth_permission"."content_type_id", "auth_permission"."codename" FROM "auth_permission" INNER JOIN "django_content_type" ON ( "auth_permission"."content_type_id" = "django_content_type"."id" ) WHERE "auth_permission"."content_type_id" IN (?) ORDER BY "django_content_type"."app_label" ASC, "django_content_type"."model" ASC, "auth_permission"."codename" ASC (1,)SELECT "django_content_type"."id", "django_content_type"."name", "django_content_type"."app_label", "django_content_type"."model" FROM "django_content_type" WHERE "django_content_type"."app_label" = ? ORDER BY "django_content_type"."name" ASC (‘admin‘,)SELECT "django_content_type"."id", "django_content_type"."name", "django_content_type"."app_label", "django_content_type"."model" FROM "django_content_type" WHERE ("django_content_type"."model" = ? AND "django_content_type"."app_label" = ?) LIMIT 21 (‘permission‘, ‘auth‘)SELECT "django_content_type"."id", "django_content_type"."name", "django_content_type"."app_label", "django_content_type"."model" FROM "django_content_type" WHERE ("django_content_type"."model" = ? AND "django_content_type"."app_label" = ?) LIMIT 21 (‘group‘, ‘auth‘)SELECT "django_content_type"."id", "django_content_type"."name", "django_content_type"."app_label", "django_content_type"."model" FROM "django_content_type" WHERE ("django_content_type"."model" = ? AND "django_content_type"."app_label" = ?) LIMIT 21 (‘user‘, ‘auth‘)SELECT "auth_permission"."content_type_id", "auth_permission"."codename" FROM "auth_permission" INNER JOIN "django_content_type" ON ( "auth_permission"."content_type_id" = "django_content_type"."id" ) WHERE "auth_permission"."content_type_id" IN (?, ?, ?) ORDER BY "django_content_type"."app_label" ASC, "django_content_type"."model" ASC, "auth_permission"."codename" ASC (2, 3, 4)SELECT "django_content_type"."id", "django_content_type"."name", "django_content_type"."app_label", "django_content_type"."model" FROM "django_content_type" WHERE "django_content_type"."app_label" = ? ORDER BY "django_content_type"."name" ASC (‘auth‘,)SELECT "django_content_type"."id", "django_content_type"."name", "django_content_type"."app_label", "django_content_type"."model" FROM "django_content_type" WHERE ("django_content_type"."model" = ? AND "django_content_type"."app_label" = ?) LIMIT 21 (‘contenttype‘, ‘contenttypes‘)SELECT "auth_permission"."content_type_id", "auth_permission"."codename" FROM "auth_permission" INNER JOIN "django_content_type" ON ( "auth_permission"."content_type_id" = "django_content_type"."id" ) WHERE "auth_permission"."content_type_id" IN (?) ORDER BY "django_content_type"."app_label" ASC, "django_content_type"."model" ASC, "auth_permission"."codename" ASC (5,)SELECT "django_content_type"."id", "django_content_type"."name", "django_content_type"."app_label", "django_content_type"."model" FROM "django_content_type" WHERE "django_content_type"."app_label" = ? ORDER BY "django_content_type"."name" ASC (‘contenttypes‘,)SELECT "django_content_type"."id", "django_content_type"."name", "django_content_type"."app_label", "django_content_type"."model" FROM "django_content_type" WHERE ("django_content_type"."model" = ? AND "django_content_type"."app_label" = ?) LIMIT 21 (‘session‘, ‘sessions‘)SELECT "auth_permission"."content_type_id", "auth_permission"."codename" FROM "auth_permission" INNER JOIN "django_content_type" ON ( "auth_permission"."content_type_id" = "django_content_type"."id" ) WHERE "auth_permission"."content_type_id" IN (?) ORDER BY "django_content_type"."app_label" ASC, "django_content_type"."model" ASC, "auth_permission"."codename" ASC (6,)SELECT "django_content_type"."id", "django_content_type"."name", "django_content_type"."app_label", "django_content_type"."model" FROM "django_content_type" WHERE "django_content_type"."app_label" = ? ORDER BY "django_content_type"."name" ASC (‘sessions‘,)SELECT "django_content_type"."id", "django_content_type"."name", "django_content_type"."app_label", "django_content_type"."model" FROM "django_content_type" WHERE ("django_content_type"."model" = ? AND "django_content_type"."app_label" = ?) LIMIT 21 (‘tag‘, ‘Core‘)SELECT "django_content_type"."id", "django_content_type"."name", "django_content_type"."app_label", "django_content_type"."model" FROM "django_content_type" WHERE ("django_content_type"."model" = ? AND "django_content_type"."app_label" = ?) LIMIT 21 (‘contenttype‘, ‘Core‘)SELECT "django_content_type"."id", "django_content_type"."name", "django_content_type"."app_label", "django_content_type"."model" FROM "django_content_type" WHERE ("django_content_type"."model" = ? AND "django_content_type"."app_label" = ?) LIMIT 21 (‘menu‘, ‘Core‘)SELECT "django_content_type"."id", "django_content_type"."name", "django_content_type"."app_label", "django_content_type"."model" FROM "django_content_type" WHERE ("django_content_type"."model" = ? AND "django_content_type"."app_label" = ?) LIMIT 21 (‘page‘, ‘Core‘)SELECT "auth_permission"."content_type_id", "auth_permission"."codename" FROM "auth_permission" INNER JOIN "django_content_type" ON ( "auth_permission"."content_type_id" = "django_content_type"."id" ) WHERE "auth_permission"."content_type_id" IN (?, ?, ?, ?) ORDER BY "django_content_type"."app_label" ASC, "django_content_type"."model" ASC, "auth_permission"."codename" ASC (8, 9, 10, 7)INSERT INTO "auth_permission" ("name", "content_type_id", "codename") SELECT ? AS "name", ? AS "content_type_id", ? AS "codename" UNION ALL SELECT ?, ?, ? UNION ALL SELECT ?, ?, ? UNION ALL SELECT ?, ?, ? UNION ALL SELECT ?, ?, ? UNION ALL SELECT ?, ?, ? UNION ALL SELECT ?, ?, ? (u‘Can a page‘, 10, ‘a_page‘, u‘Can d page‘, 10, ‘d_page‘, u‘Can d page‘, 10, ‘d_page‘, ‘can_add_%(class)s‘, 10, ‘add‘, ‘can_change_%(class)s‘, 10, ‘change‘, ‘can_delete_%(class)s‘, 10, ‘delete‘, ‘can_read_%(class)s‘, 10, ‘read‘)Traceback (most recent call last):

  

 

django 1.7+ default_permissions