guild-journal/guild/forms.py

26 lines
794 B
Python
Raw Normal View History

2023-06-10 20:32:45 +00:00
from django import forms
2023-06-19 18:01:33 +00:00
from guild.models import PlaySession, Character
class PlaySessionCharacterForm(forms.ModelMultipleChoiceField):
def label_from_instance(self, character):
if character.player:
return "{} ({})".format(character.name, character.player.name)
else:
return character.name
2023-06-10 20:32:45 +00:00
class PlaySessionForm(forms.ModelForm):
2023-06-19 18:01:33 +00:00
characters = PlaySessionCharacterForm(
queryset=Character.objects.all(), widget=forms.CheckboxSelectMultiple
)
2023-06-10 20:32:45 +00:00
class Meta:
model = PlaySession
fields = ["date", "characters", "summary"]
# set date wideget to type="date"
widgets = {
"date": forms.DateInput(attrs={"type": "date"}),
2023-06-13 20:09:25 +00:00
"summary": forms.Textarea(attrs={"rows": 32}),
2023-06-10 20:32:45 +00:00
}