I'm struggling to work out the correct solution for what I'm trying to do here and would really appreciate some help.
At the moment, I have a working system which takes a "Special" from the db and displays it in the browser. The user is able to edit that "Special" in the browser and submit it to the db. That change is then shown to the user.
The problem is that the "Special" will not update if there is no pre-existing "Special" in the db. In my views.py I have:
def changeSpecialOffer(theRequest):
myProductUuid = theRequest.POST['myProductUuid']
myNewSpecialOffer = theRequest.POST['myNewSpecialOffer']
myProduct = get_object_or_404(Product, uuid=myProductUuid)
myActiveSpecial = get_object_or_404(SpecialOffer.objects.filter(product=myProduct).filter(
active=True))
try:
myActiveSpecial.special = myNewSpecialOffer
myActiveSpecial.save()
except:
return HttpResponse(myActiveSpecial, mimetype='text/plain')
myActiveSpecial = SpecialOffer.objects.filter(product=myProduct).filter(
active=True)
return HttpResponse(myActiveSpecial, mimetype='text/plain')
I know that the reason the updating of the "Special" does not work is because the get_object_or_404 is correctly returning a 404 error as there is no pre-existing "Special" in the db.
I've been trying for a while to work out the best way to get around this problem without breaking the function in cases where there is an existing "Special" in the db.
So far, I've tried replacing the get_object_or_404 with try and except, but I then run into problems with the save function, such as 'unicode' has no attribute 'save()'.