I have custom tags for all type of fields but I can’t figure out how can I display values of “multiple select option” field on edit page:
I have this for now:
<label class="block"><select {% if field.field.widget.allow_multiple_selected %} multiple{% endif %} class="mt-1.5 w-full {% if field.errors %}border-error border{% endif %} " name="{{ field.html_name }}" id="{{ field.auto_id }}" placeholder="{{ field.label }}" {% for option in field.field.choices %}<optionvalue="{{ option.0 }}" {% if field.value == option.0 %}selected{%endif%} {{field.value}} # that displays just None </option> {% endfor %} </select> </label>
{{field.value}} displays None for multiple select option , but that’s required field and I already double check and DB has data for that field. For single select that works, and displays its value data when I try editing that field.
I just have custom field for select option fields:
Models.py:
class LoadDrivers(models.Model):
load_driver_user = models.ManyToManyField(CustomUser)
load_key = models.ForeignKey(Loads, on_delete=models.DO_NOTHING, related_name="loaddrivers")
forms.py:
class LoadDriversForm(forms.ModelForm):
class Meta:
model = LoadDrivers
fields = ['load_driver_user']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# self.fields['load_user'].widget = forms.CheckboxSelectMultiple()
self.fields['load_driver_user'].queryset = self.fields['load_driver_user'].queryset.filter(is_driver=True)
templatetags:
from django import template
from django.forms import widgets
register = template.Library()
@register.inclusion_tag('my_forms/custom_input.html')
def render_custom_input(field):
return {'field': field}
custom_input.html:
<label class="block">
<span class="font-medium text-slate-600 dark:text-navy-100">{{ field.label }} {% if field.field.required %}*{% endif %}</span>
<select
{% if field.field.widget.allow_multiple_selected %} multiple{% endif %}
class="mt-1.5 w-full {% if field.errors %}border-error border{% endif %} "
name="{{ field.html_name }}" id="{{ field.auto_id }}"
x-init="$el._tom = new Tom($el)"
{% if field.field.required %}required{% endif %}
placeholder="{{ field.label }}"
>
{% for value, label in field.field.choices %}
<option value="{{ value }}" {% if field.value|default:"" == value %} selected{% endif %} {% if value in field.field.widget.disabled_choices %} disabled{% endif %}>
{{ label }}
</option>
{% endfor %}
</select>
</label>
and I’m saying that {% if field.value|default:“” == value %} selected{% endif %} that part dont work
field.value just returns None for current field, when editing that field (editing page), but that happens for only multiselect field, single select just works perfectly.
and views.py:
def edit_load(request, load_id):
load = get_object_or_404(Loads, id=load_id)
if request.method == "POST":
# Process the form data when the request method is POST
load_drivers_form = LoadDriversForm(request.POST, instance=load)
if ( load_drivers_form.is_valid() ):
load_drivers_form.save_m2m()
else:
load_drivers_form = LoadDriversForm(instance=load)
context = {
"load_drivers_form": load_drivers_form,
}
return render(request, "loads/loads-add.html", context)
That can’t be the complete view - you’re only showing the “POST” portion and not the “GET” when it’s first retrieving the form. (It’s either that, or your indentation is inconsistent.)