1

i hope the title is enough to know what is my problem

enter image description here

This is my code in html

when I tried to save this to my database only the "6" is save just like in the picture

enter image description here

I use jquery to generate the date and textbox to input the grade

this is my html

<table  id="blacklistgrid" border="2px">
      <tr>
          <th id="th">Students Name</th>
          <th data-id='headers' id='header'>Average</th>
      </tr>
      {% for student in teacherStudents %}
      <tr class="tr2">
          <td class="td" ><input type="text" name="students" value="{{student.id}}" id="student" >{{student.Students_Enrollment_Records.Student_Users}}</td>
           <td data-id='row' id="ans"><input type='number' class='averages'  readonly/></td>
      </tr>
      {% endfor %}
</table>

<script>
     var counter = 0;
    function doTheInsert(){

        let header=$("tr#tr");  // same as $.find("tr[id='tr2']")
        $('#th').after("<th data-id='headers' id='header'><input type='date' name='date'></th>");
        var rows=$(".tr2");
        $("<td data-id='row' ><input type='number' name='gradeko' class='average' /></td>").insertAfter(".td");
        counter++;
    }
    </script>

this is my views.py

for gradeko in request.POST.get('gradeko'):
    pass

for students in request.POST.getlist('students'):
    studentss = StudentsEnrolledSubject(id=students)
    date = request.POST.getlist('date')

    V_insert_data = studentsEnrolledSubjectsGrade(
        Teacher=teacher,
        Students_Enrollment_Records=studentss,
        Date=date,
        Grade=gradeko
    )
    V_insert_data.save()

this is my model.py

class studentsEnrolledSubjectsGrade(models.Model):
    Teacher = models.ForeignKey(EmployeeUser, related_name='+', on_delete=models.CASCADE,
                                                null=True,blank=True)
    Subjects = models.ForeignKey(Subject, related_name='+', on_delete=models.CASCADE, null=True)
    Students_Enrollment_Records = models.ForeignKey(StudentsEnrolledSubject, related_name='+',on_delete=models.CASCADE, null=True)
    Grading_Categories = models.ForeignKey(gradingCategories, related_name='+', on_delete=models.CASCADE,
                                                null=True,blank=True)
    Date = models.DateField(null=True, blank=True)
    Grade = models.FloatField(null=True, blank=True)

what i tried

global gradekos, dates
    for gradeko in request.POST.getlist('gradeko'):
    gradekos = gradeko
    pass
    print(gradekos)
for date in request.POST.getlist('date'):
    dates = date
    pass
for students in request.POST.getlist('students'):
    studentss = StudentsEnrolledSubject(id=students)
    #date = request.POST.get('date')

    V_insert_data = studentsEnrolledSubjectsGrade(
        Teacher=teacher,
        Students_Enrollment_Records=studentss,
        Date=dates,
        Grade=gradekos
    )
    V_insert_data.save()

UPDATE when i tried two dates, only 1 date save in the database

enter image description here

enter image description here

for example

enter image description here

this is what I want result

enter image description here

this is the actual result

enter image description here

  • shouldn't the text field input in the form be different name ? or else you will get the last result when you loop – Linh Nguyen Jan 10 '20 at 08:14
  • yes that is why i use getlist() –  Jan 10 '20 at 08:16
  • you should try to print out gradeko in your code for debugging – Linh Nguyen Jan 10 '20 at 08:18
  • can you show me your solution sir? –  Jan 10 '20 at 08:22
  • under `for gradeko in request.POST.getlist('gradeko'):` add `print(gradeko)` - remember to indent so it's inside for loop , and check the console for what result it print out, you need to make sure what data you getting – Linh Nguyen Jan 10 '20 at 08:24
  • ahh , i get all the data what ive been input –  Jan 10 '20 at 08:25
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/205731/discussion-between-mary-rose-villaganas-orcullo-and-linh-nguyen). –  Jan 10 '20 at 08:29
  • Create an array list of all the table rows with javascript, then add them to a hidden input field using `$('#id_result').val(JSON.stringify(data_array));`. And finally retrieve the values in your view using `json.loads()` on POST submit. Information using ajax [here](https://stackoverflow.com/questions/1960240/jquery-ajax-submit-form) and another great article [here](https://www.pluralsight.com/guides/work-with-ajax-django) – Brian Dec 06 '20 at 15:19

1 Answers1

0

you setting gradekos = gradeko so it just override gradekos with your previous value(that go for dates too). So you need get all values of each object inside an array and loop through it to get the actual position values

gradekos = []
dates = []
for gradeko in request.POST.getlist('gradeko'):
    gradekos.append(gradeko)
for date in request.POST.getlist('date'):
    dates.append(date)

i=0
for single_date in dates:

   for student in request.POST.getlist('students'):
      students = StudentsEnrolledSubject(id=student)

      V_insert_data = studentsEnrolledSubjectsGrade(
      Teacher=teacher,
      Students_Enrollment_Records=students,
      Date=single_date,
      Grade=gradekos[i]
      )
      V_insert_data.save()
      i += 1
Linh Nguyen
  • 1,919
  • 1
  • 11
  • 32