from django.contrib import admin

from bejmy.categories.models import Category


@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
    list_display = (
        'name',
        'user',
        'transaction_type',
    )
    list_filter = (
        'user',
        'transaction_type',
    )
    search_fields = (
        'name',
    )
    raw_id_fields = ('parent',)

    def get_queryset(self, request, *args, **kwargs):
        queryset = super().get_queryset(request, *args, **kwargs)
        if not self.request.user.is_superuser():
            queryset = queryset.filter(user=request.user)
        return queryset

from django.contrib import admin

from bejmy.categories.models import Category

from mptt.admin import MPTTModelAdmin


@admin.register(Category)
class CategoryAdmin(MPTTModelAdmin):
    list_display = (
        'name',
        'user',
        'transaction_type',
    )
    list_filter = (
        'user',
        'transaction_type',
    )
    search_fields = (
        'name',
    )
    raw_id_fields = ('parent',)

    def get_queryset(self, request, *args, **kwargs):
        queryset = super().get_queryset(request, *args, **kwargs)
        if not self.request.user.is_superuser():
            queryset = queryset.filter(user=request.user)
        return queryset

Access to all accounts only for superusers
Access to all accounts only for superusers

  from django.contrib import admin
  
  from bejmy.categories.models import Category
  
+ from mptt.admin import MPTTModelAdmin
+ 
  
  @admin.register(Category)
- class CategoryAdmin(admin.ModelAdmin):
+ class CategoryAdmin(MPTTModelAdmin):
      list_display = (
          'name',
          'user',
          'transaction_type',
      )
      list_filter = (
          'user',
          'transaction_type',
      )
      search_fields = (
          'name',
      )
      raw_id_fields = ('parent',)
  
      def get_queryset(self, request, *args, **kwargs):
          queryset = super().get_queryset(request, *args, **kwargs)
          if not self.request.user.is_superuser():
              queryset = queryset.filter(user=request.user)
          return queryset
  
Access to all accounts only for superusers
## Code Before:
from django.contrib import admin

from bejmy.categories.models import Category


@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
    list_display = (
        'name',
        'user',
        'transaction_type',
    )
    list_filter = (
        'user',
        'transaction_type',
    )
    search_fields = (
        'name',
    )
    raw_id_fields = ('parent',)

    def get_queryset(self, request, *args, **kwargs):
        queryset = super().get_queryset(request, *args, **kwargs)
        if not self.request.user.is_superuser():
            queryset = queryset.filter(user=request.user)
        return queryset

## Instruction:
Access to all accounts only for superusers
## Code After:
from django.contrib import admin

from bejmy.categories.models import Category

from mptt.admin import MPTTModelAdmin


@admin.register(Category)
class CategoryAdmin(MPTTModelAdmin):
    list_display = (
        'name',
        'user',
        'transaction_type',
    )
    list_filter = (
        'user',
        'transaction_type',
    )
    search_fields = (
        'name',
    )
    raw_id_fields = ('parent',)

    def get_queryset(self, request, *args, **kwargs):
        queryset = super().get_queryset(request, *args, **kwargs)
        if not self.request.user.is_superuser():
            queryset = queryset.filter(user=request.user)
        return queryset
