I have a register form to log attendance at a training session, with models:
class Member(models.Model):
id = models.BigAutoField(primary_key=True)
account = models.ForeignKey('Account', verbose_name=_('account'), on_delete=models.CASCADE)
first_name = models.CharField(_('first name'), max_length=32) #, null=True, blank=True
middle_name = models.CharField(_('middle name(s)'), max_length=64, null=True, blank=True)
last_name = models.CharField(_('last name'), max_length=32) #, null=True, blank=True
known_as = models.CharField(_('known as'), max_length=32, null=True, blank=True)
…
def member_display_name(self):
return_name = str(self.first_name) + ' '
if self.known_as:
return_name += '(' + str(self.known_as) + ') '
return_name += str(self.last_name)
return return_name
def __str__(self):
return self.member_display_name()
class Register(models.Model):
id = models.BigAutoField(primary_key=True)
member = models.ManyToManyField('Member', through='Member_Register')
regdate = models.DateField(_('Date'), null=False, default=datetime.now)
regclass = models.CharField(_('Group'), max_length=64, null=False, blank=False)
…
class Member_Register(models.Model):
id = models.BigAutoField(primary_key=True)
member = models.ForeignKey(Member, null=True, on_delete=models.CASCADE)
register = models.ForeignKey(Register, verbose_name=_('register'), null=True, on_delete=models.CASCADE)
present = models.BooleanField(_('Present'), null=True, blank=True, default=False, db_index=True)
late = models.BooleanField(_('Late'), null=True, blank=True, default=False, db_index=True)
createdate = models.DateTimeField(auto_now_add=True)
moddate = models.DateTimeField(auto_now=True)
class Meta:
constraints = [
models.UniqueConstraint(fields=['member', 'register'], name='member_register duplicate prevention')
]
I have this working with a formset. I have three questions:
-
On the formset I have the fields
member
,present
andlate
from theMember_Register
model, the first defaults to aselect
and the other two are modified with widgets to be checkboxes. The select has theMember.member_display_name
displayed as theoption
(and theMember.id
at the value), enabled via theMember.__str__
override. However, I don’t really need aselect
here, I just want to showMember.member_display_name
as text (not a form element) on the form (with themember.value
(ie theMember.id
) as a hidden field). I know how to make the hidden field in the formset, but I cannot work out how to displayMember.member_display_name
– how can I get that data on to the template? -
How to sort the formset by
Member.last_name
? -
Is more of a logic query. Because the sessions are somewhat irregular the register is created by the user going to a
create register
form, where they select the date of the session and the group, submitting this form populates the register with theMember
s, and redirects to the register. Is this a reasonable way to create such registers?
Thanks