About Model Choice Fields using Models with Slugs
Use the to_field_name
attribute for forms.ModelChoiceField
and forms.ModelMultipleChoiceField
if you want to operate with fields other than the primary key.
class TagFilterForm(forms.Form):
tags = forms.ModelMultipleChoiceField(
label=_("Tags"),
queryset=Tag.objects.all(),
to_field_name="slug",
required=False,
)
Tips and Tricks Programming Django 4.2 Django 3.2 Django 2.2
Also by me
Django Paddle Subscriptions app
For Django-based SaaS projects.
Django App for You
Django GDPR Cookie Consent app
For Django websites that use cookies.
Django App for You