How to solve Django ValueError: Field 'id' expected a number

I am making filters in Django to select data from a database. Now it looks like:

view.py:

def get_current_user(request):
    current_user = request.user
    return current_user


def is_valid_query_param(param):
    return param != '' and param is not None


def bootstrapFilterView(request):
    user = get_current_user(request)
    qs = CompletedWork.objects.filter(checked_by_head=True)
    struct_divisions = StructuralDivisions.objects.filter(
        Q(head=user) |
        Q(management_unit__head=user) |
        Q(curator=user) |
        Q(management_unit__curator=user)
    )
    workers = CustomUser.objects.filter(
        Q(struct_division__head=user) |
        Q(struct_division__management_unit__head=user) |
        Q(struct_division__curator=user) |
        Q(struct_division__management_unit__curator=user)
    )
    workstype = WorksType.objects.filter(
        Q(available_to__head=user) |
        Q(available_to__curator=user)
    ).distinct()

    work_notes_contains_query = request.GET.get('work_notes_contains')
    work_scope_min = request.GET.get('work_scope_min')
    work_scope_max = request.GET.get('work_scope_max')
    period_min = request.GET.get('period_min')
    period_max = request.GET.get('period_max')
    struct_division = request.GET.get('struct_division')
    worker = request.GET.get('worker')
    work_done = request.GET.get('work_done')

    if is_valid_query_param(work_notes_contains_query):
        qs = qs.filter(work_notes__icontains=work_notes_contains_query)

    if is_valid_query_param(work_scope_min):
        qs = qs.filter(work_scope__gte=work_scope_min)

    if is_valid_query_param(work_scope_max):
        qs = qs.filter(work_scope__lte=work_scope_max)

    if is_valid_query_param(period_min):
        qs = qs.filter(period__date__gte=period_min)

    if is_valid_query_param(period_max):
        qs = qs.filter(period__date__lte=period_max)

    if is_valid_query_param(struct_division) and struct_division != 'Choose...':
        qs = qs.filter(worker__struct_division__name=struct_division)

    if is_valid_query_param(worker) and worker != 'Choose...':
        qs = qs.filter(worker=worker)

    if is_valid_query_param(work_done) and work_done != 'Choose...':
        qs = qs.filter(work_done__name=work_done)

    context = {
        'queryset': qs,
        'struct_divisions': struct_divisions,
        'workers': workers,
        'workstype': workstype,
    }
    return render(request, 'bootstrap_form.html', context)

All works well, except:

if is_valid_query_param(worker) and worker != 'Choose...':
            qs = qs.filter(worker=worker)

It's return ValueError at /boot/ Field 'id' expected a number but got 'Chambers Jessica '. I tried different syntax, but nothing helped How can I pick up id or pk from worker? Or is there another way?

My guess is your worker = request.GET.get('worker') line yields a string and worker field is a ForeignKey. request.GET.get('worker') should either be an int of worker primary key or do a query before the problematic line to find the instance: worker = Worker.objects.get(param=request.GET.get('worker'))

I expanded and wrote a separate logic for "worker" like this:

worker = request.GET.get('worker')

if worker and worker != 'Choose...':
    middle_name = None

    try:
        last_name, first_name, middle_name = worker.split()
        try:
            worker_get_id = CustomUser.objects.get(last_name=last_name,
                                                   first_name=first_name,
                                                   middle_name=middle_name)
        except  ValueError:
            worker_get_id = None

    except ValueError:
        last_name, first_name = worker.split()
        worker_get_id = CustomUser.objects.get(last_name=last_name,
                                               first_name=first_name)
Back to Top