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

  • SEARCH
  • Home
  • 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 471811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:01:25+00:00 2026-05-13T00:01:25+00:00

I am trying to save a modelform that represents a bank account but I

  • 0

I am trying to save a modelform that represents a bank account but I keep getting a ValueError even though the form appears to validate. The models I have to use are:

class Person(models.Model):
   name = models.CharField()

class Bank(models.Model):
    bsb = models.CharField()
    bank_name = models.CharField()  
    def __unicode__(self):
        return '%s - %s', (self.bank_name, self.bsb)
    def _get_list_item(self):
        return self.id, self
    list_item = property(-get_list_item)

class BankAccount(models.Model):
    bank = models.ForignKey(Bank)
    account_name = models.CharField()
    account_number = models.CharField()  

class PersonBankAcc(models.Model):
    person = models.ForeignKey(Person)

The ModelForm for the personBankAcc;

def PersonBankAccForm(forms.ModelForm):
    bank = forms.ChoiceField(widget=SelectWithPop)
    class Meta:
        model = PersonBankAcct
        exclude = ['person']

    def __init__(self, *args, **kwargs):
        super(PersonBankAccForm, self).__init__(*args, **kwargs)
        bank_choices = [bank.list_item for banks in Bank.objects.all()]
        bank_choices.isert(0,('','------'))
        self.fields['bank'].choices = bank_choices

The view is:

def editPersonBankAcc(request, personBankAcc_id=0):
    personBankAcc = get_object_or_404(PersonBankAcc, pk=personBankAcc_id)
    if request.method == 'POST':
        form = PersonBankAccForm(request.POST, instance=personBankAcc )
        if form.is_valid():
            print 'form is valid'
            form.save()


         return HttpResponseRedirect('editPerson/' + personBankAcc.person.id +'/')
     else:
         form = PersonBankAccForm(instance=personBankAcc )
     return render_to_response('editPersonBankAcc', {'form': form})

When I try to save the form I get the a VlaueError exception even though it gets passed the form.is_valid() check, the error I get is:
Cannot assign "u'26'": PersonBankAcc.bank must be a "bank" instance

I know the issue is arising because of the widget I am using in the PersonBankAccForm:
bank = forms.ChoiceField(widget=SelectWithPop)
because if I remove it it works. But all that does is gives me the ability to add a new bank to the database via a popup and then inserts it into the select list, similar to the admin popup forms. I have checked the database and the new bank is added. But it fails even if I don’t change anything, if I call the form and submit it, I get the same error.

I don’t understand why it does not fail at the is_valid check.

Any help would be greatly appreciated.

Thanks
Andrew

  • 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-13T00:01:25+00:00Added an answer on May 13, 2026 at 12:01 am

    better yet, i don’t think it really needs to be in the init function…

    def PersonBankAccForm(forms.ModelForm):
        bank = forms.ModelChoiceField(queryset=Bank.objects.all(),widget=SelectWithPop(),empty_label='-----')
        class Meta:
            model = EmpBankAcct
            exclude = ['person']
    
        def __init__(self, *args, **kwargs):
            super(PersonBankAccForm, self).__init__(*args, **kwargs)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to save a ModelForm as I did a thousand times before, but
Im trying to save a simple script in netbeans to htdocs, but i only
When trying to save the database using boost serialization, I encounter the segfault that
I'm trying to save some data into an array but unfortunately all the data
I'm trying to save my Photo class that has a byte[] File field. When
I am trying to save multiple image using this code. But instead its saving
I am trying to change a form's instance before I save it. I need
I'm trying to create a form where users can save their progress. I have
I'm getting the following error when trying to submit a form: argument of type
I'm trying to create a form that will handle video file uploads from the

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.