Django without models view in common user admin even if you have permissions and are in the same group

Asked

Viewed 485 times

1

Good afternoon, So I’m trying to give permissions that already come in Jango to an ordinary user who even belonging to the group cannot access the models that are granted to him. Someone had this problem of not viewing models in admin. python version 2.7 and Django 1.10

I already have the conf of Settings as 'Django.contrib.auth','Django.contrib.auth.middleware.Authenticationmiddleware'

in the models I have just:

class Paciente(models.Model):
    nome = models.CharField(max_length=60)
    data_nascimento = models.DateField()
    def __str__(self):
        return self.nome
    class Meta:
        verbose_name = "Paciente"
        verbose_name_plural = "Pacientes"

and in admin I have:

class PacienteAdmin(ModelAdmin):
    list_display = ('nome','data_nascimento')

admin.site.register(Paciente, PacienteAdmin)

just that and still no visualization of it. thanks in advance.

1 answer

1

I did a test with your code and it works perfectly, the only error I see in the code you posted here is in the fragment (admin.py):

class PacienteAdmin(ModelAdmin):
   list_display = ('nome','data_nascimento')

Change the line:

class PacienteAdmin(ModelAdmin):

To:

class Paciente(models.Model):

If it doesn’t work, try to follow the steps:

1) (command line)

$ django-admin startproject myproject .

2) (command line)

$ ./manage.py migrate

3) (command line)

$ ./manage.py createsuperuser

4) (command line)

$ ./manage.py startapp myapp

After these steps, the tree of your project should look like the figure below:

Árvore do projeto

5) Edit the models.py file in the myapp directory to look like this:

from django.db import models

class Paciente(models.Model):
    nome = models.CharField(max_length=60)
    data_nascimento = models.DateField()
    def __str__(self):
        return self.nome
    class Meta:
        verbose_name = "Paciente"
        verbose_name_plural = "Pacientes"

6) Edit the admin file in myapp, so it looks like this:

from django.contrib import admin
from .models import Paciente

class PacienteAdmin(admin.ModelAdmin):
    list_display = ('nome','data_nascimento')

admin.site.register(Paciente, PacienteAdmin)

7) Edit the file Settings.py to add myapp, then the INSTALLED_APPS section should look like this:

INSTALLED_APPS = [
    'myapp',    
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

After these steps, run the project with runserver, enter admin, create a group and a user, give permission for the user to include but not to delete patients. If I made no mistake posting here and Voce followed all the steps correctly, the result will be as expected.

Obs.: The example expressed here has been tested (and approved :-)) in a Linux environment.

  • Many thanks, solved the problem, I ended up creating another project and made the steps to check where the error was.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.