pre-populating partial Initial data in a Django formset

796 views Asked by At

I’m having difficulty implementing initial data in my django formset.

For context, I’m building an attendance app where a list of students exist and need to be assessed for attendance every day.

What I’m trying to do is have an administrator click on a link which has a date listed on it. They will then be taken to data grid where each row represents the number of students in the system along with 4 columns (student name, date, present/absent drop down, a notes field). The goal is to have the student name field be pre-populated with the the list of students in the Student model, the date field be pre-populated with the date on the link the user clicked and the attendance and notes fields be user inputs.

Any help would be much appreciated

Thanks!

Student model

class Student(models.Model):
    first_name = models.CharField(max_length=255)
    last_name = models.CharField(max_length=255)
    GENDER_CHOICES = (
        ('male', 'male'),
        ('female', 'female'),
    )
    gender =  models.CharField(max_length=12, choices=GENDER_CHOICES, null=False, blank=False)
    date_of_birth = models.DateField(auto_now=False, auto_now_add=False)

    @property
    def full_name(self):
        return ''.join([self.first_name, '_', self.last_name])

    def __unicode__(self):
        return self.full_name

Attendance model

class Attendance(models.Model):
    student = models.ForeignKey('students.Student')
    attendance_date = models.DateField(auto_now=False, auto_now_add=False)
    STATUS_CHOICES = (
        ('present', ‘1’),
        ('absent', ‘0’),
    )
    status =  models.CharField(max_length=12, choices=STATUS_CHOICES, null=False, blank=False)
    notes = models.CharField(max_length=300, null=True, blank=True)

    class Meta: 
        unique_together = ("student", "attendance_date")

    def __unicode__(self):
        return self.student

Attendance form

class AttendanceForm(forms.ModelForm):
    class Meta:
        model = Attendance
        fields = ["student","attendance_date", "status", "notes"] 


View

def add_attendance(request):
    s = Student.objects.all().values()
    AttendanceFormSet = formset_factory(AttendanceForm, extra=0)
    formset = AttendanceFormSet(request.POST or None, initial=s)
    if formset.is_valid():
        try:
            instance = form.save(commit=False)
            instance.save()
            return HttpResponseRedirect('/') 
        except:
            return HttpResponseRedirect('/')
    context = {
        "formset": formset,
    }
    return render(request, "group_attendance.html", context)


Template

 <table id="formset" class="form">
 {{ formset.management_form }}

{% for form in formset.forms %}
  {% if forloop.first %}
  <thead><tr>
    {% for field in form.visible_fields %}
    <th>{{ field.label|capfirst }}</th>
    {% endfor %}
  </tr></thead>
  {% endif %}
  <tr>
  {% for field in form.visible_fields %}
    <td>
    {# Include the hidden fields in the form #}
    {% if forloop.first %}
      {% for hidden in form.hidden_fields %}
      {{ hidden }}
      {% endfor %}
    {% endif %}
      {{ field.errors.as_ul }}
      {{ field }}
    </td>
  {% endfor %}
  </tr>
{% endfor %}
</table>
1

There are 1 answers

0
aronadaal On

I'm not really sure, if I understood your question the intended way. I think you're looking for a way to generate dynamic forms, which were pre-filled with data from the db.

A really helpful article about that, can be found here https://jacobian.org/writing/dynamic-form-generation/

You don't have a fixed numbers of students (-> fields in form), so you have to generate as many fields as needed. So you have to iterate over the students and create a form field for every single one.

You can find this in the article above. Just down below is a code snippet and an explanation of it:

class UserCreationForm(forms.Form):
    username = forms.CharField(max_length=30)
    password1 = forms.CharField(widget=forms.PasswordInput)
    password2 = forms.CharField(widget=forms.PasswordInput)

    def __init__(self, *args, **kwargs):
        extra = kwargs.pop('extra')
        super(UserCreationForm, self).__init__(*args, **kwargs)

        for i, question in enumerate(extra):
            self.fields['custom_%s' % i] = forms.CharField(label=question)