WayBehind
WayBehind

Reputation: 1697

Django Update Table Immediately After Save()

I'm trying to insert an order info into two tables Order and User

I can save the values for both tables but getting an error when trying to update the Order table with the User ID from the record that was just created in the User table.

Error:

Cannot assign "54L":"Order.user" must be a "User" instance.

view.py

if request.method == 'POST':
    form = OrderForm(request.POST)
    userform = UserForm(request.POST)

        if form.is_valid():

            #save both tables
            userform.save()
            form.save()

            #get user ID 
            user_info = User.objects.filter(email=request.POST['email']).order_by('-id')[0]

            #update order with user ID
            u = Order.objects.filter().order_by('-id')[0]
            u.user = user_info.id
            u.save()

            return HttpResponseRedirect('/success/')
else:
    form = OrderForm()
    ...

Upvotes: 0

Views: 231

Answers (1)

Brisc Bogdan
Brisc Bogdan

Reputation: 111

change u.user = user_info.id to u.user = user_info

Upvotes: 1

Related Questions