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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:15:47+00:00 2026-05-25T12:15:47+00:00

I have a page that displays multiple Formsets, each of which has a prefix.

  • 0

I have a page that displays multiple Formsets, each of which has a prefix. The formsets are created using formset_factory the default options, including extra=1. Rows can be added or deleted with JavaScript.

If the user is adding new data, one blank row shows up. Perfect.

If the user has added data but form validation failed, in which case the formset is populated with POST data using MyFormset(data, prefix='o1-formsetname') etc., only the data that they have entered shows up. Again, perfect. (the o1 etc. are dynamically generated, each o corresponds to an “option”, and each “option” may have multiple formsets).

However if the user is editing existing data, in which case the view populates the formset using MyFormset(initial=somedata, prefix='o1-formsetname') where somedata is a list of dicts of data that came from a model in the database, an extra blank row is inserted after this data. I don’t want a blank row to appear unless the user explicitly adds one using the JavaScript.

Is there any simple way to prevent the formset from showing an extra row if the initial data is set? The reason I’m using initial in the third example is that if I just passed the data in using MyFormset(somedata, prefix='o1-formsetname') I’d have to do an extra step of reformatting all the data into a POSTdata style dict including prefixes for each field, for example o1-formsetname-1-price: x etc., as well as calculating the management form data, which adds a whole load of complication.

One solution could be to intercept the formset before it’s sent to the template and manually remove the row, but the extra_forms attribute doesn’t seem to be writeable and setting extra to 0 doesn’t make any difference. I could also have the JavaScript detect this case and remove the row. However I can’t help but think I’m missing something obvious since the behaviour I want is what would seem to be sensible expected behaviour to me.

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-05-25T12:15:48+00:00Added an answer on May 25, 2026 at 12:15 pm

    I’ve come up with a solution that works with Django 1.1. I created a subclass of BaseFormSet that overrides the total_form_count method such that, if initial forms exist, the total does not include extra forms. Bit of a hack perhaps, and maybe there’s a better solution that I couldn’t find, but it works.

    class SensibleFormset(BaseFormSet):
        def total_form_count(self):
            """Returns the total number of forms in this FormSet."""
            if self.data or self.files:
                return self.management_form.cleaned_data[TOTAL_FORM_COUNT]
            else:
                if self.initial_form_count() > 0:
                    total_forms = self.initial_form_count()
                else:
                    total_forms = self.initial_form_count() + self.extra
                if total_forms > self.max_num > 0:
                    total_forms = self.max_num
                return total_forms
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a page that displays a table in two different modes. In each
I have a page that displays search results and has a DOM like the
I have a web page that displays dynamic data which changes every 2 seconds.
I have created a grid view that displays six cells of content. In each
I have this web page I'm developing that displays multiple images of one product
I have a multiple html tables on a page which each have a header
I have a CFWINDOW which displays multiple drop downs that are binded to respective
I have a page which displays multiple (usually 10) embedded videos. The videos use
I have a page that displays a list with a of elements with a
i have a page that displays large datasets into html tables. how can i

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.