Reputation: 1697
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