Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7573865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:17:24+00:00 2026-05-30T16:17:24+00:00

In attempting to programmatically post a new ForeignKey object through an inline formset, I’m

  • 0

In attempting to programmatically post a new ForeignKey object through an inline formset, I’m receiving an error: ValueError: invalid literal for int() with base 10: ''.

Here’s the code of my test (bloated for the sake of brevity here):

def test_merits_can_be_added(self):
    self.c = Client()
    self.c.login(username=self.user.username, password='dummy')
    self.post_data = {
      'name':u'Unhappy SE',
      'concept':u'Sad clown',
      'merit-TOTAL_FORMS':u'1',
      'merit-MAX_NUM_FORMS':u'',
      'merit-INITIAL_FORMS':u'1',
      'merit-0-id':u'',
      'merit-0-level':u'2',
      'merit-0-character':u'1',
      'merit-0-trait':u'11',
      'merit-0-specializations':u'Sometimes'
    }
    sheet = GeistCharacterSheet.objects.create(name='Happy SE', user=self.user)
    response = self.c.post(sheet.get_absolute_url(), self.post_data, follow=True)
    self.assertEqual(GeistCharacterSheet.objects.get(pk=1).chosentrait_set.all().filter(trait__name='Common Sense')[0].level, 2)
    self.assertEqual(GeistCharacterSheet.objects.get(pk=1).chosentrait_set.all().filter(trait__name='Common Sense')[0].specializations, u'Sometimes')

The view code (again, trimmed for brevity):

def character_sheet(request, sheet_id=None):
  charsheet = GeistCharacterSheet.objects.get(pk=sheet_id, user=request.user)
  if request.method == 'POST':
    sheet_form = GeistCharacterSheetForm(request.POST, instance=charsheet)          
    merit_formset = setup_merit_form(charsheet, post=request.POST)

    if sheet_form.is_valid() and merit_formset.is_valid():
      sheet_form.save()
      merit_formset.save()
      return redirect('/character-manager/list/')

def setup_merit_form(charsheet, post=None):
  MeritFormSet = inlineformset_factory(GeistCharacterSheet, ChosenTrait, form=ChosenMeritForm, extra=1)
  if post:
    return MeritFormSet(post, instance=charsheet, queryset=ChosenTrait.objects.filter(trait__trait_type__name='Merit'), prefix='merit')
  else:
    return MeritFormSet(instance=charsheet, queryset=ChosenTrait.objects.filter(trait__trait_type__name='Merit'), prefix='merit')

Here’s the traceback from the test execution:

Traceback (most recent call last):
  File "C:\charon_sheet\..\charon_sheet\character_manager\tests.py", line 119, in test_skills_can_be_changed
    response = self.c.post(sheet.get_absolute_url(), self.post_data, follow=True)
  File "C:\Program Files\BitNami DjangoStack\apps\django\django\test\client.py", line 449, in post
    response = super(Client, self).post(path, data=data, content_type=content_type, **extra)
  File "C:\Program Files\BitNami DjangoStack\apps\django\django\test\client.py", line 259, in post
    return self.request(**r)
  File "C:\Program Files\BitNami DjangoStack\apps\django\django\core\handlers\base.py", line 111, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "C:\charon_sheet\..\charon_sheet\character_manager\views.py", line 29, in character_sheet
    merit_formset = setup_merit_form(charsheet, post=request.POST)
  File "C:\charon_sheet\..\charon_sheet\character_manager\views.py", line 69, in setup_merit_form
    return MeritFormSet(post, instance=charsheet, queryset=ChosenTrait.objects.filter(trait__trait_type__name='Merit'), prefix='merit')
  File "C:\Program Files\BitNami DjangoStack\apps\django\django\forms\models.py", line 682, in __init__
    queryset=qs)
  File "C:\Program Files\BitNami DjangoStack\apps\django\django\forms\models.py", line 415, in __init__
    super(BaseModelFormSet, self).__init__(**defaults)
  File "C:\Program Files\BitNami DjangoStack\apps\django\django\forms\formsets.py", line 47, in __init__
    self._construct_forms()
  File "C:\Program Files\BitNami DjangoStack\apps\django\django\forms\formsets.py", line 108, in _construct_forms
    self.forms.append(self._construct_form(i))
  File "C:\Program Files\BitNami DjangoStack\apps\django\django\forms\models.py", line 691, in _construct_form
    form = super(BaseInlineFormSet, self)._construct_form(i, **kwargs)
  File "C:\Program Files\BitNami DjangoStack\apps\django\django\forms\models.py", line 437, in _construct_form
    connection=connections[self.get_queryset().db])
  File "C:\Program Files\BitNami DjangoStack\apps\django\django\db\models\fields\subclassing.py", line 53, in inner
    return func(*args, **kwargs)
  File "C:\Program Files\BitNami DjangoStack\apps\django\django\db\models\fields\subclassing.py", line 53, in inner
    return func(*args, **kwargs)
  File "C:\Program Files\BitNami DjangoStack\apps\django\django\db\models\fields\__init__.py", line 306, in get_db_prep_lookup
    value = self.get_prep_lookup(lookup_type, value)
  File "C:\Program Files\BitNami DjangoStack\apps\django\django\db\models\fields\__init__.py", line 292, in get_prep_lookup
    return self.get_prep_value(value)
  File "C:\Program Files\BitNami DjangoStack\apps\django\django\db\models\fields\__init__.py", line 479, in get_prep_value
    return int(value)
ValueError: invalid literal for int() with base 10: ''

I can post models, forms, more of the view, whatever anyone thinks would be helpful.

The issue is with the 'merit-0-id':u'' post item. I’ve tried with and without unicode, using 0 or -1, using False, using 'new' (a complete wild shot, I know).

My major confusion comes in that the form works when I’m running the server. I’ve examined the POST variables when I submit the form, and that id field can be empty and the item is added just fine.

Why is the form balking when it’s submitted via a test runner?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:17:25+00:00Added an answer on May 30, 2026 at 4:17 pm

    Initial forms in an inline formset need to tie back to existing models in the DB. Your setup doesn’t create the related ChosenTrait instance (which is the pk you should be using for merit-0-id). If you are testing creating all new models then 'merit-INITIAL_FORMS' should be 0.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to run a series of commands programmatically, read the error codes, and
I am attempting to programmatically generate javadocs via an instance of Runtime through the
I am attempting to programmatically monitor the size of a SQL Server database, so
When attempting to compile my C# project, I get the following error: 'C:\Documents and
Using Facebox's documentation , I'm attempting to pop up Facebox programmatically, with the Facebox
Attempting what would appear to be a simple pre ajax-post validation against a short
Attempting to deserialize JSON data and update each object's prototype and inherit a common
Attempting to build an interface and generics based graph and getting an odd error
I'm attempting to access the Closure Compiler tool programmatically , but having issues both
I'm attempting to programmatically add several Tiles which extend from TextViews into a RelativeLayout

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.