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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:29:25+00:00 2026-06-02T14:29:25+00:00

I’m trying to use a Django generated form with a AJAX call for the

  • 0

I’m trying to use a Django generated form with a AJAX call for the data population. I have just a plain ModelForm working but when I add an inlineformset to it as well I get lost as to what values I should populate in some of the hidden id fields. Every submit is creating a new instance. For a School with an Address I have this in my saving view :

... get school logic here ...
if school_form.is_valid():
    school = school_form.save(commit=False)
    address_formset = AddressFormSet(request.POST, instance=school)
    if address_formset.is_valid():
        school_form.save()
        address_formset.save()
... redirection logic here ...

This is the JS populating my form. #id_address_set-0-school and #id_address_set-0-id are the 2 Django generated fields.

$('a[name^="editSchool"]').click( function(e) {
  e.preventDefault();
  $.ajax({
    url: '/schooljson/' + this.getAttribute('schoolid') + '/'
  }).done( function(data) {
    $('#id_school_code').val(data.school_code);
    $('#id_full_name').val(data.school_name);
    $('#id_student_count').val(data.school_students);
    $('#id_school_pk').val(data.school_pk);
    $('#id_address_set-0-address_line_1').val(data.address_line_1);
    $('#id_address_set-0-address_city').val(data.address_city);
    $('#id_address_set-0-address_state').val(data.address_state);
    $('#id_address_set-0-address_zip').val(data.address_zip);
    $('#id_address_set-0-school').attr('value', data.school_pk);
    $('#id_address_set-0-id').attr('value', data.address_pk);
    $('#addSchoolModal').modal('show');
  });

Any ideas what I should be putting there?

  • 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-02T14:29:28+00:00Added an answer on June 2, 2026 at 2:29 pm

    If you already have a school instance available, then it seems like it would be easier to bind the data to the formset in the view. The following code should allow you to not have to worry about the id’s, hidden fields, etc, because the formset will already have all that handled.

    When you POST the formset you already have the code

    school = school_form.save(commit=False)
    address_formset = AddressFormSet(request.POST, instance=school)
    

    so instead of populating with json data, why not

    views.py
    def school_address(request, school_id):
        school = School.objects.get(pk=school_id)
        address_formset = AddressFormSet(instance=school)
        return render(request, "school/school_address.html",
                      {'address_formset': address_formset})
    

    In the original template (school.html)? with the link for editing the school, you would have an element for where the formset will go. (Presumably you already passes the school object to the template, and have a submit button, etc.)

    school.html

    <form>
    <a href="" id="edit_school" schoolid="{{ school.id }}">Edit</a>
    {{ school_form }}
    <div id="school_address"></div>
    </form>
    

    school_address.html can be as simple as

    {{ address_formset }}
    

    now in the javascript, the success function will drop school_address.html right inline with the school form, populated with everything you need.

    $("#edit_school").click(function(e){
      e.preventDefault();
      $.ajax({
        url: '/school_address/' + this.getAttribute('schoolid') + '/'
        success: function(data){
          $("#school_address").html(data);
        }
      });
    });
    

    Side note – I don’t think that schoolid as an anchor tag attribute will validate as a proper attribute, but I understand, “it works”. I usually store something like schoolid in a hidden input to access it in js, although there may be a more elegant way

    <input type="hidden" id="schoolid" value="{{ school.id }} />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,

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.