I have the following models and formset:
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 = self.time.date().weekday()
return weekdays[index]
def resp_name(self):
return self.resp.name
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")}"
class AltidenUser(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
phone = models.CharField(max_length=8)
is_ks = models.BooleanField(verbose_name="Is kitchen staff", default=False)
is_mu = models.BooleanField(verbose_name="is meal user", default=False)
is_staff = models.BooleanField(verbose_name="is staff", default=False)
from .models import Participant, Meal
from django.forms import inlineformset_factory
ParticipantFormSet = inlineformset_factory(Meal, Participant,
fields={"eat_in", "takeaway"},
extra=0,
labels={
"eat_in": "Spis i Netværket",
"takeaway": "Takeaway"
},
)
What I’m looking to do is having an authenticated user select the options available (eat in, takeaway or both) from a list of meals. So I’ve made an inline form set modifying the Participant table with Meal as the foreign key. How do I do this in the template? That is, make a list of meals with checkboxes to sign up the options the meal offers?
Best, Mads