添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am trying to add view_amodel permission to my models. I decided to add the permission after migration. So I made following approach.

At an_app/ init .py

from an_app.apps import MyAppConfig
default_app_config = MyAppConfig

At an_app/apps.py

from django.apps import AppConfig
from django.db.models.signals import post_migrate
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import Permission
def add_view_permissions(sender, **kwargs):
    This syncdb hooks takes care of adding a view permission too all our
    content types.
    # for each of our content types
    for content_type in ContentType.objects.all():
        # build our permission slug
        codename = "view_%s" % content_type.model
        # if it doesn't exist..
        if not Permission.objects.filter(content_type=content_type, codename=codename):
            # add it
            Permission.objects.create(content_type=content_type,
                                      codename=codename,
                                      name="Can view %s" % content_type.name)
            print "Added view permission for %s" % content_type.name
class MyAppConfig(AppConfig):
    def ready(self):
        post_migrate.connect(add_view_permissions, sender=self)

When I do python manage.py migrate, I get following error,

AttributeError: type object 'MyAppConfig' has no attribute 'rpartition'

How to solve it.

The reference to the AppConfig in the app's __init__.py is supposed to be a string, not the class itself.

Specify

default_app_config = 'an_app.apps.MyAppConfig'

and remove the import.

See the documentation.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.