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 7984455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:16:26+00:00 2026-06-04T11:16:26+00:00

I need to overrid the constructor of a form: if I have some value

  • 0

I need to overrid the constructor of a form: if I have some value in input, some fields has to be ChoiceField, otherwise they have to be CharField. here’s the code:

class AnagraficaForm(forms.Form):

    usertype = ((1,'Privato'),(0,'Libero professionista/Azienda'))
    nome = forms.CharField(max_length=100)
    cognome = forms.CharField(max_length=100)
    telefono = forms.CharField(max_length=50,required=False)
    email= forms.EmailField(max_length=100,required=False)
    indirizzo = forms.CharField(max_length=100)
    nazione = forms.ChoiceField(choices = util.get_countries_tuple_list())
    provincia = forms.CharField(max_length=100)
    citta = forms.CharField(max_length=100)
    cap = forms.CharField(max_length=10)
    codfisc = ITSocialSecurityNumberField(required=False)
    piva = ITVatNumberField(required=False)
    ragsociale = forms.CharField(max_length=100,required=False)
    is_privato = forms.TypedChoiceField(
        initial=1,
        coerce=lambda x: bool(int(x)),
        choices=usertype,
        #using custom renderer to display radio buttons on the same line
        widget=forms.RadioSelect(renderer=HorizRadioRenderer, attrs={"id":"is_privato"})
    )

    def __init__(self, country_name = 'ITALIA', region_name = 'MILANO', city_name = 'MILANO', zipcode = '', *args, **kwargs):
        if country_name != 'ITALIA':
            print 'in if'
            self.nazione = forms.ChoiceField(choices = util.get_countries_tuple_list())
            self.provincia = forms.CharField(max_length=100, initial = region_name)
            self.citta = forms.CharField(max_length=100, initial = city_name)
            self.cap = forms.CharField(max_length=10, initial = zipcode)
            kw = {'initial':{'nazione': util.get_country_id(country_name)}}
            kw.update(kwargs)
            return super(AnagraficaForm, self).__init__(*args, **kw)
        else:
            print 'in else'
            self.nazione = forms.ChoiceField(choices = util.get_countries_tuple_list())
            self.provincia = forms.ChoiceField(choices = util.get_regions_tuple_list(util.get_country_id(country_name)))
            self.citta = forms.ChoiceField(choices = util.get_cities_tuple_list(util.get_country_id(country_name), util.get_region_code(region_name)))
            self.cap = forms.ChoiceField(choices = util.get_zips_tuple_list(util.get_country_id(country_name), util.get_region_code(region_name), city_name))
            initial = {
                        'nazione' : 'IT',
                        'provincia' : util.get_region_code(region_name),
                        'citta' : util.get_region_from_cityname(city_name),
                        'cap' : util.get_city_id(zipcode)
                      }
            kw = {'initial': initial}
            kw.update(kwargs)
            return super(AnagraficaForm, self).__init__(*args, **kw)

but this way doesn’t work. even if I declare before fields as CharField and then I ovverride them in init, they will not be renderized in template (I got this message: ).

any help?

thanks

  • 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-06-04T11:16:28+00:00Added an answer on June 4, 2026 at 11:16 am

    You should manipulate self.fields[name] after form creation instead of touching self.field before it. For example, instead of:

    self.nazione = forms.ChoiceField(choices = util.get_countries_tuple_list())
    super(AnagraficaForm, self).__init__(*args, **kw)
    

    There should be (btw, __init__ should return nothing):

    super(AnagraficaForm, self).__init__(*args, **kw)
    self.fields['nazione'] = forms.ChoiceField(choices = util.get_countries_tuple_list())
    

    Or, if both fields are the same type, just modify necessary attributes:

    self.fields['nazione'].choices = util.get_countries_tuple_list()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need some regular expressions help. So far I have my code working to allow
So basically I have a Linked List class that has all of the constructor
I have a FormWizard where I need data from the first form to pass
I have a Form named ScanFolder , and I need another Form, that needs
I have a windows form analog clock and I need to create a web
I need to override the connection in my new table adapters, as they are
I need to override styles for two EditText instances. I have two issues regarding
I need a custom dialog to appear on button press. Here is my code:
I have a base controller which is defined as follows, but the constructor that
I have a simple Widget that I've created using a ContentPart. The ContentPart has

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.