Rachel Church
Rachel Church

Reputation: 268

TypeError when trying to access formset.cleaned_data.get['field']

I am trying to loop over all the forms in the formset and get all of the values but am receiving the following error:

TypeError:'builtin_function_or_method' object is not subscriptable'

The traceback shows that the correct values are there after Post but says the line that causes the error is :

time = cd.get['LunchDuration']

The values of cd are:

 {'DELETE': False,
 'EndTime': datetime.time(3, 30),
 'LunchDuration': 6,
 'LunchTime': datetime.time(3, 30),
 'StartTime': datetime.time(3, 30)}

I followed the post Django accessing formset data but an error is being thrown before setting a local variable to the post data.

Here is my code in Form.py:

def new_schedule(request):
if request.method == 'POST': 
    RNform = RNFormSet(request.POST, prefix='RN')
    if RNform.is_valid():
        nurses = []
        for form in RNform:
            cd = form.cleaned_data
            time = cd.get['LunchDuration']
            nurses.append(NurseSchedule(
                StartTime=cd.get['StartTime'],
                LunchTime=cd.get["LunchTime"],
                LunchDuration=cd.get["LunchDuration"],
                EndTime=cd.get["EndTime"]
            ))
        context = {'RNSet': nurses}
        return render(request, 'generate_schedule.html', context)

Upvotes: 2

Views: 280

Answers (1)

alecxe
alecxe

Reputation: 474231

You need to use parenthesis for the get() method calls. Replace:

cd.get['StartTime']

with:

cd.get('StartTime')

Upvotes: 4

Related Questions