Django - 基数为 10 的 int() 的 Formset 无效文字:''

Posted

技术标签:

【中文标题】Django - 基数为 10 的 int() 的 Formset 无效文字:\'\'【英文标题】:Django - Formset invalid literal for int() with base 10: ''Django - 基数为 10 的 int() 的 Formset 无效文字:'' 【发布时间】:2014-01-18 03:41:37 【问题描述】:

我的表单中有一个内联字段,我正在尝试通过 save_formset() 函数访问它

这是我的代码:

def save_formset(self, request, form, formset, change):
    periods = formset.save(commit=False)
    match = periods[0].match
    match_utils = MatchUtils()
    teamstat_utils = TeamStatUtils()
    is_update = match.reported
    match_type = match.tournament.type

    # Find positions
    first_place = match_utils.findPositions(periods,match.blue_team,match.grey_team,match.black_team,1)
    second_place = match_utils.findPositions(periods,match.blue_team,match.grey_team,match.black_team,2)
    third_place = match_utils.findPositions(periods,match.blue_team,match.grey_team,match.black_team,3)

    # Update Match
    match.first_place = first_place
    match.second_place = second_place
    match.third_place = third_place
    match.blue_periods_won = match_utils.findTeamPeriodsWon(periods,'blue_team')
    match.grey_periods_won = match_utils.findTeamPeriodsWon(periods,'grey_team')
    match.black_periods_won = match_utils.findTeamPeriodsWon(periods,'black_team')
    match.reported = True

    # Update team stats
    teamstat_utils.updateTeamStats(match.blue_team,match_utils.findPositionForTeam(match.blue_team,first_place,second_place,third_place),
        match.blue_periods_won,match.grey_periods_won+match.black_periods_won,is_update,match_type) #Blueteam
    teamstat_utils.updateTeamStats(match.grey_team,match_utils.findPositionForTeam(match.grey_team,first_place,second_place,third_place),
        match.grey_periods_won,match.blue_periods_won+match.black_periods_won,is_update,match_type) #Greyteam
    teamstat_utils.updateTeamStats(match.black_team,match_utils.findPositionForTeam(match.black_team,first_place,second_place,third_place),
        match.black_periods_won,match.blue_periods_won+match.grey_periods_won,is_update,match_type) #Blackteam

    pdb.set_trace()
    match.save()
    formset.save()

但我不断收到: int() 以 10 为底的无效文字:''

我不知道为什么,我尝试了 formset.is_valid() 并且它返回 True...

谢谢, 阿拉

编辑:忘记发布 Traceback:

    Environment:


    Request Method: POST
    Request URL: http://localhost:8000/admin/acpkinballmanageevents/match/14/?_changelist_filters=tournament__id__exact%3D5

    Django Version: 1.6.1
    Python Version: 2.7.6
    Installed Applications:
    ('djangocms_admin_style',
     'django.contrib.admin',
     'django.contrib.auth',
     'django.contrib.contenttypes',
     'django.contrib.sessions',
     'django.contrib.messages',
     'django.contrib.staticfiles',
     'django.contrib.humanize',
     'django.contrib.sites',
     'localflavor',
     'south',
     'activelink',
     'reversion',
     'autoslug',
     'ajax_select',
     'django_object_actions',
     'blog',
     'acpkinballmanageleague',
     'acpkinballmanageteams',
     'acpkinballmanageevents',
     'acpkinballmanagemembers')
    Installed Middleware:
    ('django.contrib.sessions.middleware.SessionMiddleware',
     'django.middleware.common.CommonMiddleware',
     'django.middleware.csrf.CsrfViewMiddleware',
     'django.contrib.auth.middleware.AuthenticationMiddleware',
     'django.contrib.messages.middleware.MessageMiddleware',
     'django.middleware.clickjacking.XFrameOptionsMiddleware')


    Traceback:
    File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
      114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
    File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in wrapper
      432.                 return self.admin_site.admin_view(view)(*args, **kwargs)
    File "C:\Python27\lib\site-packages\django\utils\decorators.py" in _wrapped_view
      99.                     response = view_func(request, *args, **kwargs)
    File "C:\Python27\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func
      52.         response = view_func(request, *args, **kwargs)
    File "C:\Python27\lib\site-packages\django\contrib\admin\sites.py" in inner
      198.             return view(request, *args, **kwargs)
    File "C:\Python27\lib\site-packages\reversion\revisions.py" in do_revision_context
      300.                     return func(*args, **kwargs)
    File "C:\Python27\lib\site-packages\django\utils\decorators.py" in _wrapper
      29.             return bound_func(*args, **kwargs)
    File "C:\Python27\lib\site-packages\django\utils\decorators.py" in _wrapped_view
      99.                     response = view_func(request, *args, **kwargs)
    File "C:\Python27\lib\site-packages\django\utils\decorators.py" in bound_func
      25.                 return func(self, *args2, **kwargs2)
    File "C:\Python27\lib\site-packages\django\db\transaction.py" in inner
      339.                 return func(*args, **kwargs)
    File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in change_view
      1231.                 self.save_related(request, form, formsets, True)
    File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in save_related
      884.             self.save_formset(request, form, formset, change=change)
    File "C:\Users\Ara\Documents\Sites\kinball\acpkinballmanageevents\admin.py" in save_formset
      138.      match.save()
    File "C:\Python27\lib\site-packages\django\forms\models.py" in save
      618.         return self.save_existing_objects(commit) + self.save_new_objects(commit)
    File "C:\Python27\lib\site-packages\django\forms\models.py" in save_new_objects
      747.             self.new_objects.append(self.save_new(form, commit=commit))
    File "C:\Python27\lib\site-packages\django\forms\models.py" in save_new
      873.             obj.save()
    File "C:\Python27\lib\site-packages\django\db\models\base.py" in save
      545.                        force_update=force_update, update_fields=update_fields)
    File "C:\Python27\lib\site-packages\django\db\models\base.py" in save_base
      573.             updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
    File "C:\Python27\lib\site-packages\django\db\models\base.py" in _save_table
      654.             result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
    File "C:\Python27\lib\site-packages\django\db\models\base.py" in _do_insert
      687.                                using=using, raw=raw)
    File "C:\Python27\lib\site-packages\django\db\models\manager.py" in _insert
      232.         return insert_query(self.model, objs, fields, **kwargs)
    File "C:\Python27\lib\site-packages\django\db\models\query.py" in insert_query
      1511.     return query.get_compiler(using=using).execute_sql(return_id)
    File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
      897.         for sql, params in self.as_sql():
    File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py" in as_sql
      855.                 for obj in self.query.objs
    File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py" in get_db_prep_save
      350.                                       prepared=False)
    File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py" in get_db_prep_value
      342.             value = self.get_prep_value(value)
    File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py" in get_prep_value
      1073.         return int(value)

    Exception Type: ValueError at /admin/acpkinballmanageevents/match/14/
    Exception Value: invalid literal for int() with base 10: ''

编辑: 添加模型:

tournament = models.ForeignKey(Tournament,blank=False,verbose_name="tournoi")
category = models.ForeignKey(Category,blank=False,verbose_name="catégorie")
match_num = models.CharField(max_length=2,blank=False,verbose_name="numéro de match")
time = models.TimeField(blank=False,verbose_name="heure")
plateau = models.CharField(max_length=15,blank=False,verbose_name="plateau")
blue_team = models.ForeignKey(Team,blank=False,related_name="blue_team",verbose_name="équipe bleu")
grey_team = models.ForeignKey(Team,blank=False,related_name="greye_team",verbose_name="équipe gris")
black_team = models.ForeignKey(Team,blank=False,related_name="black_team",verbose_name="équipe noir")
blue_periods_won = models.CharField(max_length=2,editable=False)
grey_periods_won = models.CharField(max_length=2,editable=False)
black_periods_won = models.CharField(max_length=2,editable=False)
first_place = models.ForeignKey(Team,related_name="first_place",default=None,editable=False,null=True)
second_place = models.ForeignKey(Team,related_name="second_place",default=None,editable=False,null=True)
third_place = models.ForeignKey(Team,related_name="third_place",default=None,editable=False,null=True)
reported = models.BooleanField(editable=False)
blue_roster = models.ForeignKey(Roster,related_name='blue_team_roster',null=True,blank=True)
grey_roster = models.ForeignKey(Roster,related_name='grey_team_roster',null=True,blank=True)
black_roster = models.ForeignKey(Roster,related_name='black_team_roster',null=True,blank=True)

类周期(models.Model):

PERIOD_NUM_CHOICES = [(i,i) for i in range(1,8)]
SCORE_CHOICES = [(i,i) for i in range(25)]

match = models.ForeignKey(Match,blank=False,verbose_name="partie")
period_num = models.PositiveSmallIntegerField(blank=False,verbose_name="numéro de période",choices=PERIOD_NUM_CHOICES)
ot = models.BooleanField(blank=False,default=False,verbose_name='prolongation')
second_place_ot = models.BooleanField(blank=False,default=False,verbose_name='prolongation pour la 2ième place')
blue_score = models.PositiveSmallIntegerField(blank=True,choices=SCORE_CHOICES)
grey_score = models.PositiveSmallIntegerField(blank=True,choices=SCORE_CHOICES)
black_score = models.PositiveSmallIntegerField(blank=True,choices=SCORE_CHOICES)

【问题讨论】:

请提供异常堆栈跟踪 已编辑!抱歉忘记了! 【参考方案1】:

您没有发布模型的详细信息。发生这种错误的原因可能是:

    您的模型中声明了 CharField,但您传递的是一个整数 或者,您有一个 IntegerFieldblank=True,但不要为其提交值。

第二个问题是因为当你声明blank = True 时,Django 将存储并返回一个空字符串。

还有很多可以在这里添加...

发布您的模型可能有助于我们了解究竟是什么问题。

【讨论】:

我刚刚添加了我的模型。谢谢!【参考方案2】:

您需要检查模板中的 URL。您需要将整数 id 传递给 URL user.id,因为 url 需要在模板中具有整数值。

例如。网址:- /polls/user.id/ 希望这对其他人有用。

【讨论】:

以上是关于Django - 基数为 10 的 int() 的 Formset 无效文字:''的主要内容,如果未能解决你的问题,请参考以下文章

django ValueError:以 10 为基数的 int() 的无效文字:''

Django 引发 ValueError: int() 的无效文字,在 POST 上以 10 为基数

Django:无法将空表单字段保存到数据库 - 以 10 为基数的 int() 的无效文字:''

以 10 为基数的 int() 的无效文字:Django 网页上的“Micro”

在 Django 的详细视图中使用两个参数时,得到“ValueError:int() 的无效文字,基数为 10:'Trancel'”

Django celery 4 - ValueError: int() 的无效文字,当启动 celery worker 时,基数为 10