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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:41:32+00:00 2026-05-31T01:41:32+00:00

I’m using an inline formset with some javascript to add more forms with a

  • 0

I’m using an inline formset with some javascript to add more forms with a button press.
One of the views keeps trying to validate blank, dynamically added forms and is triggering “this field is required” warnings. I have no problem with the initial 2 forms in the formset that are created via extra=2, they can be blank without causing errors.

OrderLineItem has a foreign key to Order

#views.py
def my_new_form_view(request):
    OrderFormSet = inlineformset_factory(Order, OrderLineItem, extra=2, can_delete=False,
                       formfield_callback=make_custom_datefield, form=OrderLineItemForm,
                       formset=OrderLineFormSet)
    if request.method == 'POST':
        form = OrderForm(request.POST)
        order_formset = OrderFormSet(request.POST, prefix="lineitem")
        if form.is_valid() and order_formset.is_valid():
            new_order = form.save()
            new_order_formset = order_formset.save(commit=False)
            order = Order.objects.get(pk=new_order.pk)
            for form in order_formset.forms:
                if form.has_changed():
                    new_f = form.save(commit=False)
                    new_f.order = order
                    new_f.save()
            return redirect() #go to details 
    else:
        form = OrderForm()
        order_formset = OrderFormSet(prefix='lineitem')

–

#forms.py
class OrderLineFormSet(forms.models.BaseInlineFormset):
    def clean(self):
        #just a clean method to look for duplicates

class OrderLineItemForm(ModelForm):
    #just some callbacks, widget setttings, and the Meta

–

#html
<!-- form stuff above this -->
{{ order_formset.management_form }}
{{ order_formset.non_form_errors }}
{% for form in order_formset %}
<div class="line_item">
    <p>&nbsp;</p>
    {{ form.non_field_errrors }}
    {% for field in form.visible_fields %}
    <div class="field">
        {{ field.errors }}
        {{ field.label_tag }}
        {{ field }}
    </div>
    {% endfor %}
    {% for hidden in form.hidden_fields %}
    {{ hidden }}
    {% endfor %}
</div>
{% endfor %}

$('#add_more').click(function() {
    cloneMore('div.line_item:last', 'lineitem');
}); 

–

#js CloneMore
function cloneMore(selector, type) {
    var newElement = $(selector).clone();
    var total = $('#id_' + type + '-TOTAL_FORMS').val();
    newElement.find(':input').each(function() {
        var name = $(this).attr('name').replace('-' + (total-1) + '-','-' + total + '-');
        var id = 'id_' + name;
        $(this).attr({'name': name, 'id': id}).val('').removeAttr('checked');
    });
    newElement.find('label').each(function() {
        var newFor = $(this).attr('for').replace('-' + (total-1) + '-','-' + total + '-');
        $(this).attr('for', newFor);
    });
    total++;
    $('#id_' + type + '-TOTAL_FORMS').val(total);
    $(selector).after(newElement);
}

I’ll add more code on request, this is just already a lot of clutter.

Something with the javascript added forms seems to be triggering the .has_changed() despite being blank. What am I missing that isn’t allowing the blank forms?


Solution

One field in the formset has a default. When I clone a form it clears all inputs. Django sees the removal of the default value as a change and triggers the is_changed() and validates the form.

  • 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-31T01:41:33+00:00Added an answer on May 31, 2026 at 1:41 am

    Check the generated form code in something like Firebug. If any field has a value, the form will be treated as “filled out”, in the sense that it will then attempt validation on it. Even if you have hidden fields with default values, this can occur. For the form to be disregarded, it must be completely blank on submission.

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

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
I'm making a simple page using Google Maps API 3. My first. One marker
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small

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.