Django-Formset对以10为底的int()的文本无效:'';

Django-Formset对以10为底的int()的文本无效:'';,django,save,admin,formset,Django,Save,Admin,Formset,我的表单中有一个内联字段,我试图通过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_upd

我的表单中有一个内联字段,我试图通过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()
但我一直得到:基数为10的int()的文本无效:“”

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

谢谢, 阿糖胞苷

编辑:忘记发布回溯:

    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):


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

  • 在模型中声明了一个
    CharField
    ,但传递的是一个整数
  • 或者,您有一个
    IntegerField
    ,其中
    blank=True
    不提交值
  • 第二个问题是,当您声明
    blank=True
    Django时,它将存储并返回一个空字符串

    这里还有很多可以添加的内容


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

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

  • 在模型中声明了一个
    CharField
    ,但传递的是一个整数
  • 或者,您有一个
    IntegerField
    ,其中
    blank=True
    不提交值
  • 第二个问题是,当您声明
    blank=True
    Django时,它将存储并返回一个空字符串

    这里还有很多可以添加的内容


    发布您的模型可能有助于我们了解问题的具体原因。

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

    例如url:-/polls/{{user.id}/
    希望这对其他人有用。

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

    例如url:-/polls/{{user.id}/
    希望这对其他人有用。

    提供异常堆栈跟踪,请输入!对不起,忘了!提供异常堆栈跟踪,请将其输入!对不起,忘了!我刚刚添加了我的模型。谢谢我刚刚添加了我的模型。谢谢
    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)