Get the user logged in to Django Admin

Asked

Viewed 666 times

1

Good morning, you guys.

I have a small application made entirely in Django Admin and I need to get the user logged in to make a specific query listing registered customers and and other models by this specific user/company so as to separate the application by group of users.

How can I do this with Django Admin? Because from what I read around the request and the instance only work in the views and how I am using only Django Admin as CRUD do not use views.

I tried to put the request inside the models.Manager and I get an exception:

class ClienteManager(models.Manager):
def get_queryset(self, request):
    return super(ClienteManager, self).get_queryset().filter(cliente=True).filter(escritorio=request.user.username)

TypeError at /admin/cadastro/cliente/
get_queryset() missing 1 required positional argument: 'request'

2 answers

1

Hello Alison,

Your question is very broad, but let’s see if I can help you: Django Admin comes with several tools disabled, you will have to enable them as you need. The command you need will be for the list is 'list.display' but if it’s to do a search ,because I looked at you using the queryset, is the 'search_fields'.
You will use this command in the app 'admin.py'

Modeladmin.get_search_fields(request)

The get_search_fields method is Given the Httprequest and is expected to Return the same Kind of Sequence type as for the search_fields attribute.

class QuestionAdmin(admin.ModelAdmin):
    ordering = ['date_created']
    search_fields = ['question_text']

Modeladmin.list_display

Set list_display to control which Fields are displayed on the change list page of the admin.

Example:

class PersonAdmin(admin.ModelAdmin):
     list_display = ('first_name', 'last_name')

Examples of Django admin tools and commands: Exemplos de ferramentas do Django Admin

Extra: Django admin has several pre-set templates, such as password reset and others, in case you want to overwrite some template, such as changing color, you will have to figure out what the name of view at the root, there are two ways to edit it, first edit at the root(problem) and second create a view with the same name in your urls.py.. But that’s another story.

Source:

Things You Should Know About Django Admin for Your App to Be Great

Medium: Things You Must Know About Django Admin As Your App Gets Bigger

Dajngo documentation specifies the subject:

Django Admin Site

I hope you’ve helped, and the force is with you.

0

Whoa, buddy. So what I really wanted was to filter logs per logged-in user so as to split the application to more than one user so that one does not see the record of the other.

I managed to make this filter with the following code:

# Fonte: https://stackoverflow.com/questions/9574788/filter-django-admin-by-logged-in-user
class FilterUserAdmin(admin.ModelAdmin): 
    def save_model(self, request, obj, form, change):
        form.cleaned_data['escritorio'] = request.user.id
        super().save_model(request, obj, form, change)

    def get_queryset(self, request):
        if not request.user.is_superuser:
            qs = super(FilterUserAdmin, self).get_queryset(request) 
            return qs.filter(escritorio__user=request.user)
        else:
            qs = super(FilterUserAdmin, self).get_queryset(request) 
            return qs.all()

    def has_change_permission(self, request, obj=None):
        if not obj:
            # the changelist itself
            return True
        return obj.escritorio.user == request.user

It was working as I wanted, only now I came across another problem I’m still trying to solve which is to make this filter work also in forms that have foreign keys because I realized that the filter works pretty in the forms without foreign key, but when he has some foreign camp he carries all the records from inside the bank.

Browser other questions tagged

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