Search code examples
djangodjango-admindjango-admin-actions

Django Admin. Giving permission to a user or a group and on save, I have 500 server error


update

Briefly ran app in debug mode... On save it gave me the following:

ProgrammingError at /admin/auth/user/4/change/
syntax error at or near "ON"
LINE 1: ...ser_groups" ("user_id", "group_id") VALUES (4, 5) ON CONFLIC...

Looked up the related fault and sure enough it is question of updating PostgreSQL from 9.2 to the latest version...


update

Why I cannot find how to assign users to groups, assign default permissions for users/groups from the terminal? Logically, that would be some kind of solution to this problem... not perfect, but at least some solution?

Well, I guess I can do it in database itself through sql queries...


If I try to assign the user to a group or assign permission to a group or a user, I have 500 server error fault.

I have the following models.py, it's in: /home/e/sc_project/sc It's autogenerated, it tells me the following message for every table in the database:

This is an auto-generated Django model module.
# You'll have to do the following manually to clean this up:
#   * Rearrange models' order
#   * Make sure each model has one field with primary_key=True
#   * Make sure each ForeignKey and OneToOneField has `on_delete` set to the desired behavior
#   * Remove `managed = False` lines if you wish to allow Django to create, modify, and delete the table
# Feel free to rename the models, but don't rename db_table values or field names.
from django.db import models
# Unable to inspect table 'auth_group'
# The error was: syntax error at or near "WITH ORDINALITY"
LINE 6:                     FROM unnest(c.conkey) WITH ORDINALITY co...
                                                  ^
# Unable to inspect table 'auth_group_permissions'
# The error was: syntax error at or near "WITH ORDINALITY"
LINE 6:                     FROM unnest(c.conkey) WITH ORDINALITY co...
                                                  ^

and so on... That's because I have 9.2 PostgreSQL database(?)... Can it give me the problems with the permissions? If it does, any work around it? I cannot update to later PostgreSQL because c-panel doesn't support later versions, which means I will be able to reach data only using psql, which is not so nice as through the c-panel...

The system works fine on a local server(PostgreSQL 12), but fails on a remote centos 7 based server, which run by nginx+gunicorn.

Is it possible that I have to switch on permission folder for gunicorn? I didn't see any clear answers on it. There are answers for permissions for gunicorn itself, but not for folder for permissions. So far it's just my wild guess, based on the same approach to static or media files. But it really doesn't give me any fault messages on journalctl -u gunicorn command.

Besides this... I checked my models.py, it completely and fully correlates to PostgreSQL database. As per Django manual, I don't have to create standard permissions like create, delete, view they are handled by 'django.contrib.auth'.

In settings.py I have 'django.contrib.auth' in INSTALLED_APPS. And as I understand it should take care of the user, group, permissions many to many relationships automatically.

I have django version 3.1.1, so it is quite up to date. So here shouldn't be any problem.

What else could be?

All users and groups are giving me no errors on create, delete... All models are imported. Here is my models.py file:

class AuthGroup(models.Model):
    name = models.CharField(unique=True, max_length=150)

    class Meta:
        managed = False
        db_table = 'auth_group'


class AuthGroupPermissions(models.Model):
    group = models.ForeignKey(AuthGroup, models.DO_NOTHING)
    permission = models.ForeignKey('AuthPermission', models.DO_NOTHING)

    class Meta:
        managed = False
        db_table = 'auth_group_permissions'
        unique_together = (('group', 'permission'),)


class AuthPermission(models.Model):
    name = models.CharField(max_length=255)
    content_type = models.ForeignKey('DjangoContentType', models.DO_NOTHING)
    codename = models.CharField(max_length=100)

    class Meta:
        managed = False
        db_table = 'auth_permission'
        unique_together = (('content_type', 'codename'),)


class AuthUser(models.Model):
    password = models.CharField(max_length=128)
    last_login = models.DateTimeField(blank=True, null=True)
    is_superuser = models.BooleanField()
    username = models.CharField(unique=True, max_length=150)
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=150)
    email = models.CharField(max_length=254)
    is_staff = models.BooleanField()
    is_active = models.BooleanField()
    date_joined = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'auth_user'


class AuthUserGroups(models.Model):
    user = models.ForeignKey(AuthUser, models.DO_NOTHING)
    group = models.ForeignKey(AuthGroup, models.DO_NOTHING)

    class Meta:
        managed = False
        db_table = 'auth_user_groups'
        unique_together = (('user', 'group'),)


class AuthUserUserPermissions(models.Model):
    user = models.ForeignKey(AuthUser, models.DO_NOTHING)
    permission = models.ForeignKey(AuthPermission, models.DO_NOTHING)

    class Meta:
        managed = False
        db_table = 'auth_user_user_permissions'
        unique_together = (('user', 'permission'),)

In my admin.py I don't have any of the above tables, but as I understand they are already taken care of automatically when the project was created.

My PostreSQL database looks like this: Each table has:

--Columns
---list of columns
--Browse
---!Error Loading display.php&subject=table&server=...Allow&database=sc_sc&schema=public&action=tree&table=auth_group(200:OK)
--Indexes
---gives me indexes of PKs and unique keys
--Constraints
---gives me constraints
--Triggers
---No objects found (I didn't use triggers)
--Rules
---No objects found (Same)
--Admin
--Info
--Privileges
--Import
--Export

Pretty much standard for every table in database.


Solution

  • Well, after all considerations... I believe...

    If you have some kind of similar fault due to conflicts among versions of PostgreSQL, Django and cpanel... You can add default permissions and assign users to groups using sql queries in database itself...

    You can do sql script and it would be not slower than through admin side of the site if not quicker...

    If any other ideas, please let me know...

    Thank you