OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to add permissions in django admin panel for user?

  • Thread starter Thread starter mightycode Newton
  • Start date Start date
M

mightycode Newton

Guest
I have a django app. And I added in the admin panel of django the following permission:

Code:
Accounts | account | Can view account

And in the code of admin.py of the accounts app. I added this:

Code:
from django.contrib.auth import get_user_model
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType

from .models import Account
# from .models import *

# Register your models here.
User = get_user_model()

user = User.objects.get(email='[email protected]')
print(user.has_perm('view_account'))
content_type = ContentType.objects.get_for_model(Account)
permission = Permission.objects.get(
    codename='view_account', content_type=content_type)
user.user_permissions.add(permission)

permissions = Permission.objects.filter(content_type=content_type)

for perm in permissions:
    print(f"Codename: {perm.codename}, Name: {perm.name}")
print(user)


class AccountAdmin(UserAdmin):
    list_display = (
        "email",
        "first_name",
        "last_name",
        "username",
        "last_login",
        "date_joined",
        "is_active",
    )
    list_display_links = ("email", "first_name", "last_name")
    filter_horizontal = (
        'groups',
        'user_permissions',
    )

    readonly_fields = ("last_login", "date_joined")
    ordering = ("-date_joined",)

    list_filter = ()

    User = get_user_model()
    user = User.objects.get(email='[email protected]')
    content_type = ContentType.objects.get_for_model(Account)
    permission = Permission.objects.get(
        codename='view_account', content_type=content_type)
    user.user_permissions.add(permission)


admin.site.register(Account, AccountAdmin)

And when I start the django app. I don't get any errors.

But when I login with the account with the user permission. I still see the message:

You don’t have permission to view or edit anything.

And the permissions:

  • Is active
  • Is staff are selected

Question: how to add permissions for users?
<p>I have a django app. And I added in the admin panel of django the following permission:</p>
<pre><code>Accounts | account | Can view account
</code></pre>
<p>And in the code of admin.py of the accounts app. I added this:</p>
<pre><code>from django.contrib.auth import get_user_model
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType

from .models import Account
# from .models import *

# Register your models here.
User = get_user_model()

user = User.objects.get(email='[email protected]')
print(user.has_perm('view_account'))
content_type = ContentType.objects.get_for_model(Account)
permission = Permission.objects.get(
codename='view_account', content_type=content_type)
user.user_permissions.add(permission)

permissions = Permission.objects.filter(content_type=content_type)

for perm in permissions:
print(f"Codename: {perm.codename}, Name: {perm.name}")
print(user)


class AccountAdmin(UserAdmin):
list_display = (
"email",
"first_name",
"last_name",
"username",
"last_login",
"date_joined",
"is_active",
)
list_display_links = ("email", "first_name", "last_name")
filter_horizontal = (
'groups',
'user_permissions',
)

readonly_fields = ("last_login", "date_joined")
ordering = ("-date_joined",)

list_filter = ()

User = get_user_model()
user = User.objects.get(email='[email protected]')
content_type = ContentType.objects.get_for_model(Account)
permission = Permission.objects.get(
codename='view_account', content_type=content_type)
user.user_permissions.add(permission)


admin.site.register(Account, AccountAdmin)

</code></pre>
<p>And when I start the django app. I don't get any errors.</p>
<p>But when I login with the account with the user permission. I still see the message:</p>
<blockquote>
<p>You don’t have permission to view or edit anything.</p>
</blockquote>
<p>And the permissions:</p>
<ul>
<li>Is active</li>
<li>Is staff
are selected</li>
</ul>
<p>Question: how to add permissions for users?</p>
 
Top