Pairing up list elements with registration form

Hi, I have the models (pasted below), for a system to register authenticated users as Participants for Meals offering eat in or takeaway (or both). I’d like to have a page which list meals along with checkboxes that corresponding what the individual meal offers (eat in and/or takeaway). I plan on extending the default authentication model by added user tables Staff and User and give the Users the ability to self register for meals and their option of eat in or takeaway as available. I do not however see how I can pair up a Meal in the list with a form for selecting the options in the template. If anyone knows of a way to do this, I would be most appreciative.

Best, Mads

from django.db import models
from django.utils.timezone import localtime
from django.conf import settings
from django.db import models
# Create your models here.

class Meal(models.Model):
    time = models.DateTimeField("Tid/Dato")
    description = models.CharField(verbose_name="Beskrivelse af måltid", max_length=300)
    deadline = models.DateTimeField(verbose_name="Tilmeldingsfrist")
    price_pp = models.DecimalField(verbose_name="Pris per person", decimal_places=2, max_digits=5)
    price_tt = models.DecimalField(verbose_name="Pris i alt", decimal_places=2, max_digits=5)
    resp = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="meal_responsibilities")
    offer_takeaway = models.BooleanField(verbose_name="Tilbyd takeaway", default=False)
    offer_eat_in = models.BooleanField(verbose_name="Tilbyd spisning i netværket", default=False)
    participants = models.ManyToManyField(settings.AUTH_USER_MODEL, through="Participant")
    def __str__(self):
        return (f"{self.description} Dato:"
                f" {localtime(self.time).date().strftime("%d/%m/%Y")} "
                f"{localtime(self.time).strftime("%H:%M")} Pris: {self.price_pp}")

    def weekday(self):
        weekdays = ["Mandag", "Tirsdag", "Onsdag", "Torsdag", "Fredag", "Lørdag", "Søndag"]
        index =

        return weekdays[index]

    def resp_name(self):

class Participant(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name="Bruger", on_delete=models.CASCADE)
    meal = models.ForeignKey(Meal, verbose_name="Måltid", on_delete=models.CASCADE)
    takeaway = models.BooleanField(verbose_name="Takeaway", default=False)
    eat_in = models.BooleanField(verbose_name="Spisning i netværket", default=False)
    registration_time = models.DateTimeField(verbose_name="Date/time registered")
    payment_due = models.DateField(verbose_name="Betales senest")
    payment_status = models.BooleanField(verbose_name="Er betalt", default=False)

    def registered_on_time(self):
        meal_record = self.meal
        return self.registration_time <= meal_record.deadline

    def __str__(self):
        return f"Bruger: {self.user}  Dato: {localtime(value=self.meal. time).strftime("%x kl. %H:%M")}"

If I’m understanding you correctly, what you’re looking for here would be an inline formset for a User, to show a form for each Meal. Each instance of that form would have whatever fields it’s supposed to have.

See Formsets | Django documentation | Django and Creating forms from models | Django documentation | Django for more details about this.

Thanks for your reply!

If I modify the Participant model with a foreign key relationship to Meal, I can’t find a way to access the Meal fields in the template, can you point me in the right direction?

Best, Mads

Why did you modify your Participant model? You already have a many-to-many relationship established between Meal and Participant.