how to access current user in django model manager

Pre*_*vik 0 django django-models

I am making a CRUD application, and I have the current piece of code in my view:

def dashboard(request):
    template = 'dashboard/index.html'
    form = CustomerForm()      
    if request.user.groups.filter(name__in=['West']).exists():
        customers = Customer.objects.filter(Q(department='630') | Q(department='635')).all()
    elif request.user.groups.filter(name__in=['North']).exists():
        customers = Customer.objects.filter(Q(department='610') | Q(department='615') | Q(department='620')).all()
    elif request.user.groups.filter(name__in=['East']).exists():
        customers = Customer.objects.filter(Q(department='660') | Q(department='655') | Q(department='650')).all()
    elif request.user.groups.filter(name__in=['South']).exists():
        customers = Customer.objects.filter(Q(department='640') | Q(department='645')).all()
    elif request.user.groups.filter(name__in=['North-West']).exists():
        customers = Customer.objects.filter(Q(department='625')).all()
    else:
        customers = Customer.objects.all()

    context = {
        "customers": customers,
        "form": form, 
        }

    return render(request, template, context)
Run Code Online (Sandbox Code Playgroud)

I have separate create, update and delete functions that also need to use the same if-statement. I learned about model-manages and created the following:

class CustomerQueryset(models.query.QuerySet):

    def customer_department_query(self):
        if request.user.groups.filter(name__in=['West']).exists():
            customers = Customer.objects.filter(Q(department='630') | Q(department='635')).all()
        elif request.user.groups.filter(name__in=['North']).exists():
            customers = Customer.objects.filter(Q(department='610') | Q(department='615') | Q(department='620')).all()
        elif request.user.groups.filter(name__in=['East']).exists():
            customers = Customer.objects.filter(Q(department='660') | Q(department='655') | Q(department='650')).all()
        elif request.user.groups.filter(name__in=['South']).exists():
            customers = Customer.objects.filter(Q(department='640') | Q(department='645')).all()
        elif request.user.groups.filter(name__in=['North-West']).exists():
            customers = Customer.objects.filter(Q(department='625')).all()
        else:
            customers = Customer.objects.all()

class CustomerManager(models.Manager):
    def get_queryset(self):
        return CustomerQueryset(self.model, using=self.db)

    def get_customer_group(self):
        return self.get_queryset().customer_department_query()
Run Code Online (Sandbox Code Playgroud)

I then found out that the model-manager can't access the request so I created the following middleware:

class GetUser(object):

    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):

        user = (request.user)
        request.current_user = user
        user_group = request.user.groups.all()
        print(user)
        print(user_group)
        response = self.get_response(request)

        return response
Run Code Online (Sandbox Code Playgroud)

When I refresh my application the shell prints both the user name and the user group but how do I access this information in the Queryset/Model Manager?? Am I even on the right path or is there a better way to do this?

Any help would be much appreciated.

Dan*_*man 5

整个方法是错误的。

您无法从管理器访问“当前用户”是有原因的,这是因为在很多情况下都没有当前用户-如果代码是从Shell或脱机任务执行的,而用户不是登录,依此类推。

更好的方法是使它成为User模型本身的方法-如果您还没有自定义用户模型,则可以使用此添加的方法定义一个简单的代理模型,并将AUTH_USER_MODEL设置为指向该类。该方法然后可以self.groups直接访问。

注意,通过使用__in而不是单独的Q对象可以使您的代码更简单,并且.all()最后不需要:

customers = Customer.objects.filter(department__in=['630', '635'])
Run Code Online (Sandbox Code Playgroud)