Question

I have an inline field in my form and I am trying to access it via the save_formset() function

Here is my code:

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

But I keep getting: invalid literal for int() with base 10: ''

I don't know why, I tried formset.is_valid() and it returns True...

Thanks, Ara

EDIT: Forgot to post 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: ''

EDIT: Adding models:

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)

class Period(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)
Was it helpful?

Solution

You didn't post the details of your model. This kind of error could happen because:

  1. you have a CharField declared in your model but you are passing an integer
  2. or, you have an IntegerField with blank=True and don't submit a value for it.

The second problem is because that when you declare blank = True Django will store and return you an empty string.

And there are a lot more could be added here...

post your models might help us to know what exactly is the problem.

OTHER TIPS

You need to check URL in your template. you need to pass the integer id to URL {{user.id}} because url need to have integer value in template.

Ex. url:- /polls/{{user.id}}/ Hope this will work for others.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top