Mutiple Forms Saved using one unique ID

I'm trying to save mutiple forms for spe_form using a unique ID for gen_format the same time using the template below by clicking on save button. Could you please help me with that?

The code works only for one save for only form of spe_form but not for many

def preps(request, prep=None):
    if prep_General:
        info = Mprep.objects.get(id=prep)
        info = Mprep()
    # Handle form submissions
    if request.method == 'POST':
        # Instantiate the forms with submitted data
        gen_form = MGForm(request.POST, instance=info)
        spe_form = MSForm(request.POST, prefix='preps')

        if gen_form.is_valid() and spe_form.is_valid():
            preps =
            preps.prep = info
            messages.success(request, 'Information saved successfully')
            return redirect('preps_view',
            messages.error(request, 'Error in saving information')

        gen_form = MGForm(instance=info)
        spe_form = MSForm(prefix='preps')

    context = {'gen_form': gen_form, 'spe_form': spe_form}
    return render(request, 'base.html', context)

<div class="container mt-5">

  <h1>Measurements Prep</h1>
  <form method="post">
    {% csrf_token %}
    <div class="form-row">
      <div class="form-group col-4">
        {{ gen_form.project|as_crispy_field }}
      <div class="form-group col-4">
        {{ gen_form.cell|as_crispy_field }}
      <div class="form-group col-4">
        {{ gen_form.sample|as_crispy_field }}
    <table id="myTable" class="table" data-toggle="table" data-mobile-responsive="true" data-pagination="true"
          <th>{{|as_crispy_field }}</th>
          <th>{{ spe_form_set.value|as_crispy_field }}</th>
          <th>{{ spe_form_set.unit|as_crispy_field }}</th>
      <button id="add-row-button" class="btn btn-primary mt-4">Add Row</button>

    <input type="submit" class="btn btn-primary mt-4" value="Save">


  document.getElementById("add-row-button").addEventListener("click", function () {
    var tableBody = document.getElementById("myTable");
    var newRow = document.createElement("tr");
    newRow.innerHTML = `
      <td>{{|as_crispy_field }}</td>
      <td>{{ spe_form_set.value|as_crispy_field }}</td>
      <td>{{ spe_form_set.unit|as_crispy_field }}</td>

You can make the id None for prep instance to save it as a new record in database:

def preps(request, prep=None):
   if request.method == 'POST':
        if gen_form.is_valid() and spe_form.is_valid():
            preps =
            preps.prep = info
   = None # <-- here
Back to Top