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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:52:12+00:00 2026-05-15T16:52:12+00:00

I’m looking for an updated version of these Django SuperForms . Can’t seem to

  • 0

I’m looking for an updated version of these Django SuperForms. Can’t seem to get it to work in Django 1.2. In particular, I’d like it to work with ModelForms.

My use case is almost identical to his; I have an Address model that I’d like to use as a sub-form in various places. It’s a pain to try and combine everything in the view func.

  • 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-15T16:52:12+00:00Added an answer on May 15, 2026 at 4:52 pm

    I’ve updated superforms.py to work w/1.2, and attached it to the ticket you linked to: http://code.djangoproject.com/attachment/ticket/3706/superform.2.py

    There’s a project I’m working on that could benefit from this, so I figured I’d spend the time and help you out as well.

    Keep in mind that I just got this to work w/1.2, and didn’t really try to clean up the internals. Now that I have test cases proving the API, I can go back and clean that up later.

    If you’re using it with ModelForms and you’d like the save() functionality you’ll have to override the method on your SuperForm class.

    I currently have it locally in my “common” repository w/~90% code coverage that covers multiple SubForms, mixed SubForms & declared forms, and ModelForms. I’ve included the test cases below (beware it uses my TestCaseBase class, but this should give you the gist of the API). Let me know if you have questions, or any areas I missed.

    from django_common.forms.superform import SuperForm, SubForm
    from django_common.test import TestCaseBase
    from django import forms
    
    class WhenSuperFormsIsUsedWithOnlySubForms(TestCaseBase):
        def get_superform_with_forms(self, post_data=None):
            class AddressForm(forms.Form):
                street = forms.CharField(max_length=255)
                city = forms.CharField(max_length=255)
    
            class BusinessLocationForm(forms.Form):
                phone_num = forms.CharField(max_length=255)
    
            class TestSuperForm(SuperForm):
                address = SubForm(AddressForm)
                business_location = SubForm(BusinessLocationForm)
    
            return TestSuperForm(data=post_data)
    
        def should_not_be_valid_with_no_data(self):
            tsf = self.get_superform_with_forms()
            self.assert_false(tsf.is_valid())
    
        def should_have_two_sub_forms(self):
            tsf = self.get_superform_with_forms()
            self.assert_equal(len(tsf.base_subforms),  2)
            self.assert_equal(len(tsf.forms), 2)
    
        def should_display_as_ul(self):
            tsf = self.get_superform_with_forms()
            self.assert_equal(tsf.as_ul(), '<li><label for="id_business_location-phone_num">Phone num:</label> <input id="id_business_location-phone_num" type="text" name="business_location-phone_num" maxlength="255" /></li>\n<li><label for="id_address-street">Street:</label> <input id="id_address-street" type="text" name="address-street" maxlength="255" /></li>\n<li><label for="id_address-city">City:</label> <input id="id_address-city" type="text" name="address-city" maxlength="255" /></li>')
    
        def should_display_as_table(self):
            tsf = self.get_superform_with_forms()
            self.assert_equal(tsf.as_table(), '<tr><th><label for="id_business_location-phone_num">Phone num:</label></th><td><input id="id_business_location-phone_num" type="text" name="business_location-phone_num" maxlength="255" /></td></tr>\n<tr><th><label for="id_address-street">Street:</label></th><td><input id="id_address-street" type="text" name="address-street" maxlength="255" /></td></tr>\n<tr><th><label for="id_address-city">City:</label></th><td><input id="id_address-city" type="text" name="address-city" maxlength="255" /></td></tr>')
    
        def should_display_as_p(self):
            tsf = self.get_superform_with_forms()
            self.assert_equal(tsf.as_p(), '<p><label for="id_business_location-phone_num">Phone num:</label> <input id="id_business_location-phone_num" type="text" name="business_location-phone_num" maxlength="255" /></p>\n<p><label for="id_address-street">Street:</label> <input id="id_address-street" type="text" name="address-street" maxlength="255" /></p>\n<p><label for="id_address-city">City:</label> <input id="id_address-city" type="text" name="address-city" maxlength="255" /></p>')
    
        def should_display_as_table_with_unicode(self):
            tsf = self.get_superform_with_forms()
            self.assert_equal(tsf.__unicode__(), tsf.as_table())
    
        def should_be_valid_if_good_data(self):
            data = {
                'business_location-phone_num' : '8055551234',
                'address-street' : '1234 Street Dr.',
                'address-city' : 'Santa Barbara',
            }
            tsf = self.get_superform_with_forms(data)
            self.assert_true(tsf.is_valid())
            self.assert_equal(tsf.cleaned_data['business_location']['phone_num'],
                              '8055551234')
            self.assert_equal(tsf.cleaned_data['address']['street'], '1234 Street Dr.')
            self.assert_equal(tsf.cleaned_data['address']['city'], 'Santa Barbara')
    
        def should_be_invalid_if_missing_data(self):
            data = {
                'business_location-phone_num' : '8055551234',
                'address-street' : '1234 Street Dr.',
            }
            tsf = self.get_superform_with_forms(data)
            self.assert_false(tsf.is_valid())
    
            self.assert_false(tsf.errors['business_location'])
            self.assert_true(tsf.errors['address'])
            self.assert_equal(tsf.errors['address']['city'], ['This field is required.'])
    
        def should_be_invalid_if_invalid_data(self):
            data = {
                'business_location-phone_num' : '8055551234',
                'address-street' : '1234 Street Dr.',
                'address-city' : '',
            }
            tsf = self.get_superform_with_forms(data)
            self.assert_false(tsf.is_valid())
    
    
    class WhenSuperformsIsUsedWithSubFormsAndDeclaredFields(TestCaseBase):
        """Some basic sanity checks that working with fields combined with SubForms works."""
        def get_superform_with_forms(self, post_data=None):
            class AddressForm(forms.Form):
                street = forms.CharField(max_length=255)
    
            class TestSuperForm(SuperForm):
                name = forms.CharField(max_length=255)
                address = SubForm(AddressForm)
    
            return TestSuperForm(data=post_data)
    
        def should_not_be_valid_with_no_data(self):
            tsf = self.get_superform_with_forms()
            self.assert_false(tsf.is_valid())
    
        def should_have_two_forms_and_a_single_subform(self):
            tsf = self.get_superform_with_forms()
            self.assert_equal(len(tsf.base_subforms),  1)
            self.assert_equal(len(tsf.forms), 2)
    
        def should_print_as_table(self):
            tsf = self.get_superform_with_forms()
            self.assert_equal(tsf.as_table(), '<tr><th><label for="id_name">Name:</label></th><td><input id="id_name" type="text" name="name" maxlength="255" /></td></tr>\n<tr><th><label for="id_address-street">Street:</label></th><td><input id="id_address-street" type="text" name="address-street" maxlength="255" /></td></tr>')
    
        def should_validate_when_fields_exist(self):
            data = {
                'name': 'Sam',
                'address-street': 'Some Street',
            }
            tsf = self.get_superform_with_forms(data)
            self.assert_true(tsf.is_valid())
    
            self.assert_equal(tsf.cleaned_data['name'], 'Sam')
            self.assert_equal(tsf.cleaned_data['address']['street'], 'Some Street')
    
        def should_not_validate_with_invalid_data(self):
            data = {
                'name': '',
                'address-street': 'Some Street',
            }
            tsf = self.get_superform_with_forms(data)
            self.assert_false(tsf.is_valid())
    
            self.assert_equal(tsf.errors['name'], ['This field is required.'])
    
    
    
    class WhenSuperformsIsUsedWithModelForms(TestCaseBase):
        def get_superform_with_forms(self, post_data=None):
            from django.db import models
            class Address(models.Model):
                city = models.CharField(max_length=255)
    
            class AddressForm(forms.ModelForm):
                class Meta:
                    model = Address
    
            class TestSuperForm(SuperForm):
                address = SubForm(AddressForm)
    
            return TestSuperForm(data=post_data)
    
        def should_not_be_valid_with_no_data(self):
            tsf = self.get_superform_with_forms()
            self.assert_false(tsf.is_valid())
    
        def should_have_two_forms_and_a_single_subform(self):
            tsf = self.get_superform_with_forms()
            self.assert_equal(len(tsf.base_subforms),  1)
            self.assert_equal(len(tsf.forms), 1)
    
        def should_print_as_table(self):
            tsf = self.get_superform_with_forms()
            self.assert_equal(tsf.as_table(), '<tr><th><label for="id_address-city">City:</label></th><td><input id="id_address-city" type="text" name="address-city" maxlength="255" /></td></tr>')
    
        def should_validate_when_fields_exist(self):
            data = {
                'address-city': 'Some City',
            }
            tsf = self.get_superform_with_forms(data)
            self.assert_true(tsf.is_valid())
    
            self.assert_equal(tsf.cleaned_data['address']['city'], 'Some City')
    
        def should_not_validate_with_invalid_data(self):
            data = {
                'address-city': '',
            }
            tsf = self.get_superform_with_forms(data)
            self.assert_false(tsf.is_valid())
    
            self.assert_equal(tsf.errors['address']['city'], ['This field is required.'])
    

    Enjoy!

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I'm looking for suggestions for debugging... If you view this site in Firefox or
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.