Inline formsets

Hello,

I need some help.

I have these class model “Registers” that have 2 FK on two different models and I’m using a modelformset_factory with the Registers model. I need the field “lesson_at” to apear only once in the form and update all the records with the same data. I’ve been looking to the inline formsets but I 'm a bit confused since I have two FK in the Registers model and in the Django documentation the example is only with one FK.

How can I do this?

Here is my code:

models.py

class Registers(models.Model):

    class Meta:
        verbose_name_plural = 'Registers'

    lesson_at = models.DateField(help_text="Data da Aula:", verbose_name="Data da Aula:")
    regclasseid = models.ForeignKey(Turmas, on_delete=models.CASCADE, verbose_name="Classe:")
    regalunoid = models.ForeignKey(Alunos, on_delete=models.CASCADE, help_text="Aluno:", verbose_name="Aluno:")

    PRESENT_ABSENT = (
        ('P', 'Present'),
        ('A', 'Absent'),
    )

    status = models.CharField(max_length=1, choices=PRESENT_ABSENT, verbose_name="Presence/Absence:")

    def __int__(self):
        return self.regalunoid

forms.py

from django import forms
from .models import Registers, Alunos


class RegisterForm(forms.ModelForm):

    regalunoid = forms.ModelChoiceField(queryset=Alunos.objects.all(), label="Aluno:",
                                        widget=forms.Select(attrs={'disabled': 'disabled'}))
    
    class Meta:
        model = Registers
        fields = ["regalunoid", "status"]
        labels = ["Alunos", "State"]

views.py

def RegisterUpdate(request, **kwargs):

    t = Turmas.objects.get(id=kwargs['pk'])

    RegisterFormSet = modelformset_factory(Registers, form=RegisterForm, extra=0)
    form = RegisterFormSet(queryset=Registers.objects.filter(regclasseid=kwargs['pk']))
    return render(request, 'schooladmin/registers.html', {'form': form, 'turma': t})

Thanks in advance

The second FK is not important here.

You have a set of Registers that are all related to a single Turmas. The Alunos that an individual Registers refers to has no effect on that Registers relationship to the Turmas.

But that’s not how your data is structured. You don’t have a lesson_at in Turmas. You have a lesson_at in Registers, and each Registers has its own instance of that field.

If you want all the Registers to have the same lesson_at, then lesson_at should not be a field in the formset. You should have a field in a form outside the formset to allow for entry of lesson_at, then use that value to populate all the individual instances of Registers with that value.

Side note: What you have effectively done here is create a many-to-many relationship between Turmas and Alunos, with Registers as the through model with additional data. Keep this concept in mind - it may facilitate some operations.

Thanks again Ken, I think I got it now. I was stuck thinking I need to have the
lesson_at field on the formset. But has you point it out it doesn’t need to be there, a simple input date on the form will do the job. :+1: