0

I am making a post via jquery to my backend. I first look to see if a pledge has already been made. If not, I create one.

@csrf_exempt
def pledgeReceive(request):
fname = request.POST.get('fname', '')
lname = request.POST.get('lname', '')
participantName = request.POST.get('participantName', '')
participantID = request.POST.get('participantID', '')
ppl = request.POST.get('ppl', '')
maxi = request.POST.get('maxi', '')

sponsor = fname + ' ' + lname
participant_obj = Participant.objects.get(pk = participantID)

try:
    pledge = Pledge.objects.get(sponsor = sponsor, participant = participant_obj, pledge_amount = ppl, max_pledge_amount = maxi, datetime = datetime.now())
except Pledge.DoesNotExist:
    pledge = Pledge(sponsor = sponsor, participant = participant_obj, pledge_amount = ppl, max_pledge_amount = maxi, datetime = datetime.now())
    pledge.save()

response = HttpResponse()
response.content = serialized_obj = serializers.serialize('json', [ pledge, ])
response['Content-Type'] = 'application/json'
return response

I get a 500 internal server error. With the following traceback:

ValidationError at /pledgeReceive/
No exception supplied
    Traceback:
File "/home/vtrelayc/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/vtrelayc/lib/python2.6/site-packages/django/views/decorators/csrf.py" in wrapped_view
  77.         return view_func(*args, **kwargs)
File "/home/vtrelayc/projects/relay/relayapp/views.py" in pledgeReceive
  461.  pledge.save()
File "/home/vtrelayc/lib/python2.6/site-packages/django/db/models/base.py" in save
  463.         self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/home/vtrelayc/lib/python2.6/site-packages/django/db/models/base.py" in save_base
  551.                 result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
File "/home/vtrelayc/lib/python2.6/site-packages/django/db/models/manager.py" in _insert
  203.         return insert_query(self.model, objs, fields, **kwargs)
File "/home/vtrelayc/lib/python2.6/site-packages/django/db/models/query.py" in insert_query
  1593.     return query.get_compiler(using=using).execute_sql(return_id)
File "/home/vtrelayc/lib/python2.6/site-packages/django/db/models/sql/compiler.py" in execute_sql
  911.         for sql, params in self.as_sql():
File "/home/vtrelayc/lib/python2.6/site-packages/django/db/models/sql/compiler.py" in as_sql
  872.                 for obj in self.query.objs
File "/home/vtrelayc/lib/python2.6/site-packages/django/db/models/fields/__init__.py" in get_db_prep_save
  873.         return connection.ops.value_to_db_decimal(self.to_python(value),
File "/home/vtrelayc/lib/python2.6/site-packages/django/db/models/fields/__init__.py" in to_python
  850.             raise exceptions.ValidationError(msg)

Exception Type: ValidationError at /pledgeReceive/

I am providing an exception though with except Pledge.DoesNotExist:?

1
  • just do a pledge.full_clean() before the save() you will know the exact error. Commented Apr 13, 2013 at 3:04

2 Answers 2

1

Why not just use get_or_create? It is a shortcut for what you are attempting to do.

pledge, created = Pledge.objects.get_or_create(sponsor = sponsor, participant = participant_obj, pledge_amount = ppl, max_pledge_amount = maxi, datetime = datetime.now())

Now you have a pledge object and a boolean, created, that lets you know whether the object was created or fetched.

Other than that, the error is probably caused by the fact that you are just getting the GET parameters without casting them to the types that your Pledge object expects. They are getting passed as strings even though most of your fields probably aren't expecting strings. Maybe use a form (for it's built in validation/type casting) or manually convert your GET parameters to the correct types.

Sign up to request clarification or add additional context in comments.

Comments

1

ValidationError exception is raised when data fails form or model field validation but because your not using form.is_valid() or any validator which validate your input values this will result to No Exception Supplied. Even your Pledge.DoesNotExist cannot catch the error because it's only check when an object is not found for the given parameters of a query.

Based on your code, I found invalid values.

ppl = request.POST.get('ppl', '')
maxi = request.POST.get('maxi', '')

If I'm not wrong the code above pass an integer. But you didn't know your passing a string not an int. It must be,

ppl = int(request.POST.get('ppl', 0))
maxi = int(request.POST.get('maxi', 0))

I don't know your model code pattern, if your passing a value for foreignkey or date your must also check that because they are very sensitive. You don't have to worry about CharField because they can handle it unless if required. But I suggest you use form, if you can't handle this error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.