小编Pre*_*vik的帖子

how to access current user in django model manager

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 …

django django-models

0
推荐指数
1
解决办法
761
查看次数

标签 统计

django ×1

django-models ×1