当我尝试保存两个对象属性的添加时,Django不断抛出以下内容。
invalid literal for int() with base 10: ''
观点是:
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()
有问题的模型是:
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
和
class Pack(models.Model):
name = models.CharField(max_length=25)
videos_allowed = models.IntegerField()
cost = models.IntegerField()
def __unicode__(self):
return self.name
回溯是:
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: ''
作为参考,user_profile.videos_remaining的存储值为1,pack.videos_allowed的存储值为100.我希望它将两个值一起添加并将它们存储为user_profile.videos_remaining对象属性中的101。
如果你能提供任何帮助我会非常感激。
编辑:问题最终是因为我没有传递整数字段last_4_digits的值。如果你有一个带有空白= True的integerField并且没有为它提交一个值,那么似乎会发生这种情况。
答案 0 :(得分:8)
问题最终是因为我没有传递整数字段last_4_digits的值。如果你有一个带有空白= True的integerField并且没有为它提交一个值,那么似乎会发生这种情况。
答案 1 :(得分:3)
我在Django人口脚本中也遇到过此错误消息,其中ForeignKey同时包含blank=True
和null=True
,并且传递了''
作为objects.get_or_create()
的参数。
我通过传递argument=None
来解决问题。 None
部分是线索。
答案 2 :(得分:0)
当“ Integer” inupt包含空或字符串值(例如“'id”,“ ...”等)时,会发生这种类型的错误。 因此,您必须找出这种输入类型所得到的。
答案 3 :(得分:0)
对我来说,问题是我正在使用required=False, disabled=True,
,但没有将数据发送到该字段的服务器。我改为
amount_payments = CharField(widget=TextInput(attrs={'readonly': 'readonly'}))