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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:44:30+00:00 2026-06-13T08:44:30+00:00

Django: 1.4.1 Model: class Hoja(models.Model): nombre = models.CharField(max_length=200) # requerido class Linea(models.Model): hoja =

  • 0

Django: 1.4.1

Model:

class Hoja(models.Model):
    nombre = models.CharField(max_length=200) # requerido

class Linea(models.Model):
    hoja = models.ForeignKey(Hoja) # requerido
    nombre = models.CharField(max_length=200) # requerido
    padre = models.ForeignKey('self', null=True, blank=True, related_name='hijo')

View:

lineas = Linea.objects.filter(hoja=alt).order_by('id')
LineaHojaSet = modelformset_factory(Linea, can_delete=True, extra=1 if request.POST.has_key('siguiente') else 0)
formset = LineaHojaSet(request.POST or None, queryset=lineas)
if request.method=='POST':
    # process formset
return render_to_response('template.html', {'formset':formset}, context_instance=RequestContext(request))

Template:

<table>
    <thead>
        <tr><th>Nombre</th><th>Borrar</th></tr>
    </thead>
    <tbody>
        {% for fs in formset %}
        <tr>
            <td>{{ fs.nombre }}</td>
            <td>{{ fs.id }}</td>
        </tr>
        {% endfor %}
    </tbody>
</table>
<input type="submit" name="siguiente" value="Añadir siguiente" />

When I submit the “siguiente” button, I can see than the formset is getting the correct extra field of 1, but in the webpage, the only rows showing are the database ones. It’s this a bug, or I’m doing something wrong?

  • 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-13T08:44:31+00:00Added an answer on June 13, 2026 at 8:44 am

    Formset factory finds number of forms either by max_num, extra parameters or form-TOTAL_FORMS parameter in request.POST (or data) from management form.

    In your case, request.POST['form-TOTAL_FORMS'] has number which does not include extra form . So it does not add extra form when you create formset.

    One solution would be to increment this number by one when your condition is met. e.g.

    data = None
    if request.POST:
        data = request.POST.copy() #required as request.POST is immutable
    if request.POST.has_key('siguiente'):
       data['form-TOTAL_FORMS'] = int(data['form-TOTAL_FORMS']) + 1
    
    #now use data instead of request.POST
    formset = LineaHojaSet(data, queryset=lineas)
    ....
    

    However, there are some drawbacks of manipulating formset this way. When you validate formset, the extra form will show errors if there are any required fields.

    Better solution would be to create formset again before passing it template with one extra form and queryset. Most likely, when formset is valid, you would save any new objects, those will get added by queryset. So your page will show newly added objects and one extra form.

    lineas = Linea.objects.filter(hoja=alt).order_by('id')
    LineaHojaSet = modelformset_factory(Linea, can_delete=True,)
    formset = LineaHojaSet(request.POST or None, queryset=lineas)
    if request.method=='POST':
        # process formset
        if formset.is_valid:
           #saved and done with formset.
           if request.POST.has_key('siguiente'):
               LineaHojaSet = modelformset_factory(Linea, can_delete=True, extra=1)
               formset = LineaHojaSet(queryset=lineas)
               ...
    return render_to_response('template.html', {'formset':formset}, context_instance=RequestContext(request))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a basic Django model like: class Business(models.Model): name = models.CharField(max_length=200, unique=True) email
Here is a Django model I'm using. class Person(models.Model): surname = models.CharField(max_length=255, null=True, blank=True)
I have the following Django model: class opetest(models.Model): name = models.CharField(max_length=200) people = models.ManyToManyField(User,
My Django model is as follow: class Page(models.Model): title = models.CharField(max_length=200) class Section(models.Model): page_id
I have a simple Django model like: class Person(models.Model): referrer = models.ForeignKey('self', null=True) ...
I have the following Django model: class Make: name = models.CharField(max_length=200) class MakeContent: make
I have the following Django model: class Product(models.Model): name = models.CharField(max_length=250) slug = models.SlugField(max_length=250,
Suppose I have a Django model as follows: class Person(models.Model): first_name = models.CharField(max_length=50) last_name
I have a Django model like this: class Registration(models.Model): appId =models.CharField(max_length = 100) registeredUser
I've this django model: from django.db import models class MyModel(models.Model): foo_it = model.CharField(max_length=100) foo_en

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.