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