3

I know this has been asked before, but for my circumstance, I can't seem to figure out why this is being thrown

When I try to run my calculations, I am given this error from my console:

ValueError: invalid literal for int() with base 10: ''

and it is saying its coming from

File "/var/sites/live_mbpathways/moneybroker/apps/investments/ajax.py", line 30, in calc_cdic
    investments = Investment.objects.all().filter(plan = plan, financial_institution=fiid, maturity_date__gte = now).order_by('maturity_date').exclude(id=investment_id)

Does anyone know why this could be happening?

Here is my ajax.py where that code is located:

@login_required
@moneybroker_auth
@csrf_exempt
def calc_cdic(request, plan, investment_id=None, *args, **kwargs):
    from investments.models import Investment
    from financial_institutions.models import FinancialInstitution
    from profiles.models import Profile
    from plans.models import Plan
    from datetime import datetime
    now = datetime.now()
    json = {}
    data = request.POST

    if request.is_ajax():
        total = 0
        fiid = data.get('financial_institution')
        amt = data.get('amount') or 0
        pay_amt = data.get('pay_amount') or 0
        mat_amt = data.get('maturity_amount') or 0
        investments = Investment.objects.all().filter(plan = plan, financial_institution=fiid, maturity_date__gte = now).order_by('maturity_date').exclude(id=investment_id)
        for i in investments:
            total += i.maturity_amount
        print total
        json['total'] = float(str(total))
        json['amt'] = float(str(amt))
        json['pay_amt'] = float(str(pay_amt))
        json['mat_amount'] = float(str(mat_amt))
        json['fiid'] = fiid
        print json

    return HttpResponse(simplejson.dumps(json), mimetype='application/json')
jterrace
  • 64,866
  • 22
  • 157
  • 202
TheLifeOfSteve
  • 3,208
  • 6
  • 37
  • 53
  • You must have redacted some code because you have multiple undefined variables. Please post the full source so we can actually see where you might be getting an empty string when an integer is expected. – Chris Pratt May 16 '12 at 14:52
  • 1
    Also, don't do `data.get('pay_amount') or 0`. The `get` method accepts a default value parameter, e.g.: `data.get('pay_amount', 0)` – Chris Pratt May 16 '12 at 14:54
  • @Chris It's not the same thing. If `data['pay_amount'] == ''`, then `data.get('pay_amount', 0)` will return '' not 0. – Daniel Roseman May 16 '12 at 14:58
  • Can you write down yor model structure? – Mp0int May 16 '12 at 14:58
  • @DanielRoseman: I didn't think about that use case. However, that's generally why I don't like code like that in the first place. It's masking logic: namely, that if the key isn't set *or* the actual value is blank, then set it to zero. – Chris Pratt May 16 '12 at 15:07

4 Answers4

5

The int() function is throwing an exception because you are trying to convert something that is not a number.

I suggest that you might consider using debugging print statements to find out what plan's and fiid's are initally and how they change.

Another thing you can do is wrap the call to int() using try/catch

val='a'
try:
    int_val = int(val)
except ValueError:
    print("Failure w/ value " + val)
octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131
3

Somewhere in that chain of calls, you're passing an empty string which the code is then trying to convert to an integer. Log your input at the time to track it down.

John Gaines Jr.
  • 11,174
  • 1
  • 25
  • 25
0

Suggest you look at the values of plan and fiid, because either plan or fiid is not set to the value that you think.

octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
0

For me, I had to delete the migration folder and follow the steps in the top answer in this link and then re-run the migrations, and then the error went away and it worked.

Community
  • 1
  • 1
Kevin Zhao
  • 2,113
  • 2
  • 14
  • 18