Search code examples
djangopython-3.xdjango-admindjango-2.1django-permissions

Django Migration from 2.0 to 2.2(View Permissions Issue)


While Migrating the Django Facing issue related to migration :

python manage.py migrate demo --database demo

Getting Error related to :

ValueError: Cannot assign "ContentType: ContentType object (1)": the current database router prevents this relation.

Detailed Error Log : https://ideone.com/z6NPkq

Tested Django Version: After Django version 2.0.13, facing this issue.

Let me know what could be the reason for this?

EDIT: Found the reason in Django 2.1, They have added view Permission: Source

Why the view permissions are not generating automatically?

Solution: In Django module, if We will move to file path: django/contrib/auth/management/__init.py

We have to change Line Number: 79

Permission(codename=codename, name=name, content_type=ct)

to

Permission(codename=codename, name=name, content_type_id=ct.id)

I have found this solution after debugging inside the Django but still unable to understand why this is happening. Let me know other solution and any reason for this solution.

EDIT-1: Looking for an answer so that I don't have to change the core Django module(Library).

Solution-1 :

def allow_relation(self, obj1, obj2, **hints):
    if obj1._state.db == "demo" or obj2._state.db=="demo":
        return True
    return obj1._state.db == obj2._state.db 

Note: In my case, Other databases are using the demo database tables as a reference, so for that in Approuter class customized the allow_relation method.


Solution

  • Seems like a router issue. It should be because you are using Django with multiple database configuration. You need to overwrite allow_relation function in app router to allow relation between the databases where content-type is present and demo database according to the example above.