diff --git a/core/forms.py b/core/forms.py index 50fd32b3..13cbeaab 100644 --- a/core/forms.py +++ b/core/forms.py @@ -8,7 +8,7 @@ from django.utils.translation import gettext as _ from taggit.forms import TagField from core import models -from core.widgets import TagsEditor +from core.widgets import TagsEditor, ChildRadioSelect def set_initial_values(kwargs, form_type): @@ -173,6 +173,7 @@ class FeedingForm(CoreModelForm, TaggableModelForm): model = models.Feeding fields = ["child", "start", "end", "type", "method", "amount", "notes", "tags"] widgets = { + "child": ChildRadioSelect, "start": forms.DateTimeInput( attrs={ "autocomplete": "off", diff --git a/core/templates/core/child_radio_option.html b/core/templates/core/child_radio_option.html new file mode 100644 index 00000000..80854e01 --- /dev/null +++ b/core/templates/core/child_radio_option.html @@ -0,0 +1,12 @@ +{% load imagekit static %} + +{% if widget.wrap_label %} + {% endif %}{% include "django/forms/widgets/input.html" %} +{% if widget.wrap_label %}{% if widget.picture %} + {% thumbnail '40x40' widget.picture as thumb %} + +{% else %} + +{% endif %} {{ widget.label }}{% endif %} diff --git a/core/widgets.py b/core/widgets.py index 1c21865e..6c39b944 100644 --- a/core/widgets.py +++ b/core/widgets.py @@ -1,6 +1,6 @@ -from django.forms import Media from typing import Any, Dict, Optional -from django.forms import Widget + +from django.forms import Widget, RadioSelect from . import models @@ -81,3 +81,13 @@ class TagsEditor(Widget): "most": [self.__unpack_tag(t) for t in most_tags], } return result + + +class ChildRadioSelect(RadioSelect): + input_type = "radio" + option_template_name = "core/child_radio_option.html" + + def create_option(self, name, value, label, selected, index, subindex=None, attrs=None): + option = super().create_option(name, value, label, selected, index, subindex, attrs) + option['picture'] = value.instance.picture + return option