3

Django keeps throwing the following when I try and save the addition of two object attributes.

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

The view is:

def buy_pack(request, pack_name):
if request.method == 'POST':
    form = CardForm(request.POST, request.user)
    pack = Pack.objects.get(name=pack_name)
    stripe_user = request.user.username

    if form.is_valid():
        token = request.POST['stripeToken']
        charge = stripe.Charge.create(
            amount=pack.cost,
            currency="usd",
            card=token,
            description=stripe_user+"_"+pack.name,
        )

        datestring = charge.created
        dt = datetime.datetime.fromtimestamp(float(datestring))
        new_purchase = Purchase(user=request.user, date_time=dt, pack=pack, payment_id=charge.id, last_4_digits=charge.card.last4)
        new_purchase.save()

        user_profile = request.user.get_profile()
        t = user_profile.videos_remaining + pack.videos_allowed
        user_profile.videos_remaining = t
        user_profile.save()

The models in question are:

class UserProfile(models.Model):
    user = models.ForeignKey(User, unique=True)
    name = models.CharField(max_length=50)
    videos_remaining = models.IntegerField(default=1)
    last_4_digits = models.IntegerField(max_length=4, blank=True)
    stripe_id = models.CharField(max_length=255, blank=True)

    def __unicode__(self):
        return self.name

And:

class Pack(models.Model):
    name = models.CharField(max_length=25)
    videos_allowed = models.IntegerField()
    cost = models.IntegerField()

    def __unicode__(self):
        return self.name

Traceback is:

Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/Users/Jeff/Dropbox/xxxxx/Code/thankyouvid/../thankyouvid/main/views.py" in buy_pack
  48.           user_profile.save()
File "/Library/Python/2.7/site-packages/django/db/models/base.py" in save
  460.         self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/Library/Python/2.7/site-packages/django/db/models/base.py" in save_base
  526.                         rows = manager.using(using).filter(pk=pk_val)._update(values)
File "/Library/Python/2.7/site-packages/django/db/models/query.py" in _update
  491.         return query.get_compiler(self.db).execute_sql(None)
File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
  869.         cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
  725.             sql, params = self.as_sql()
File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py" in as_sql
  834.                 val = field.get_db_prep_save(val, connection=self.connection)
File "/Library/Python/2.7/site-packages/django/db/models/fields/subclassing.py" in inner
  28.             return func(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/db/models/fields/subclassing.py" in inner
  28.             return func(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py" in get_db_prep_save
  276.         return self.get_db_prep_value(value, connection=connection, prepared=False)
File "/Library/Python/2.7/site-packages/django/db/models/fields/subclassing.py" in inner
  53.             return func(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/db/models/fields/subclassing.py" in inner
  53.             return func(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py" in get_db_prep_value
  271.             value = self.get_prep_value(value)
File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py" in get_prep_value
  876.         return int(value)

Exception Type: ValueError at /buy_pack/most/
Exception Value: invalid literal for int() with base 10: ''

For reference the stored value for user_profile.videos_remaining is 1 and the stored value for pack.videos_allowed is 100. I am expecting it to add the two values together and store them as 101 in the user_profile.videos_remaining object attribute.

If you can provide any help I would greatly appreciate it.

EDIT: The problem ended up being that I was not passing a value for the integer field last_4_digits. It seems that this happens if you have a integerField with blank=True and don't submit a value for it.

4 Answers 4

8

The problem ended up being that I was not passing a value for the integer field last_4_digits. It seems that this happens if you have a integerField with blank=True and don't submit a value for it.

3
  • The problem is when you have blank = True, then django will store and return you an empty string (I assume your card model stores it as a string) '' which is what you were trying to pass in as an integer. You can get the same error by doing int('') in the Python shell. Mar 24, 2012 at 13:21
  • @slipnslide21 I am trying to understand this. If an IntegerField has blank=True, then its not mandatory to submit a value for it, no?
    – Anupam
    Mar 31, 2017 at 13:03
  • Actually, got it now. @Burhan Khalid's detailed response here (stackoverflow.com/a/20399819/1526703) helped
    – Anupam
    Mar 31, 2017 at 13:07
3

I experienced this error message as well in a Django population script with a ForeignKey having both blank=True and null=True, and passing '' as argument to objects.get_or_create().

I fixed the issue by passing argument=None. The None part is the clue.

1
  • 1
    I was facing exactly the same problem in a population script. Thanks.
    – Anupam
    Mar 31, 2017 at 13:07
0

This type of error is occur when an "Integer" inupt contains empty or string value like " 'id' ", " '.'.", etc. So, you have to find out that for what this type of input you get.

0

The problem for me was that I was using required=False, disabled=True, that didn't send the data to the server for that field. I changed to

amount_payments = CharField(widget=TextInput(attrs={'readonly': 'readonly'}))

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.