Skip to content
Advertisement

django ‘dict’ object has no attribute ‘job_id’

I’ve got this code:

def create_full_data_spb(contains_name: str, language="cs", system=False) -> tuple:
    full_data = []

    labels = []
    data_pass = []
    data_fail = []
    data_skip = []

    data_duration = []
    data_estimated_duration = []
    if not system:
        queryset = BuildRunMultiJob.objects.select_related("job_id") 
                        .filter(job_id__full_display_name__contains=contains_name) 
                        .filter(language=language) 
                        .values('job_id', 'job_id__pass_count', 'job_id__fail_count', 'job_id__skip_count') 
                        .order_by('-job_id__timestamp')[:10]

    for i in queryset:
        labels.append(str(i.job_id))
        data_pass.append(i.pass_count)
        data_fail.append(i.fail_count)
        data_skip.append(i.skip_count)

        # stop using mili
        mili = int(i.estimated_duration)
        temp = datetime.datetime.fromtimestamp(mili / 1000).strftime('%H:%M:%S')
        data_estimated_duration.append(temp)

        mili = int(i.duration)
        temp = datetime.datetime.fromtimestamp(mili / 1000).strftime('%H:%M:%S')
        data_duration.append(temp)

        full_data.append([i.job_id, i.pass_count, i.skip_count, i.fail_count, temp])

    return labels, data_pass, data_fail, data_skip, data_duration, data_estimated_duration, full_data

Even though value() is defined, I get the error 'dict' object has no attribute 'job_id' I guess the error will also be for pass_count, skip_count and fail_count

What am I supposed to do here?

Advertisement

Answer

        queryset = BuildRunMultiJob.objects.select_related("job_id") 
                        .filter(job_id__full_display_name__contains=contains_name) 
                        .filter(language=language) 
                        .values('job_id', 'job_id__pass_count', 'job_id__fail_count', 'job_id__skip_count') 
                        .order_by('-job_id__timestamp')

Your query does not return a queryset with object instances, so you cannot refer to fields with a “.” It is caused by https://docs.djangoproject.com/en/4.0/ref/models/querysets/#values

You can either edit your code or remove values, only then will you get the object’s own fields too.

4 People found this is helpful
Advertisement