Reputation: 3
Whenever I try to add to_form_field to subject and membership form fields, I get the following errors:
ValueError: invalid literal for int() with base 10: 'Programming'
ValueError: invalid literal for int() with base 10: 'Free'
Field 'id' expected a number but got 'Programming'.
Field 'id' expected a number but got 'Free'.
URL without to_form_name:
http://127.0.0.1:8000/courses/?name=&min_views=&max_views=&min_date=&max_date=&expertise=&subject=1&membership=1
URL with to_form_name:
http://127.0.0.1:8000/courses/?name=&min_views=&max_views=&min_date=&max_date=&expertise=&subject=Programming&membership=Free
I would like to be able to pass the actual string rather than the number in the URL for SEO purposes. When I leave to_field_name out of forms.py, there is no error and my search form filtering works. But it puts numbers in the URL which is undesirable.
Upvotes: 0
Views: 54
Reputation: 32294
You're using to_field_name
on your ModelChoiceField
s, so you need to query by the fields that you set for those parameters
if subject_query:
qs = qs.filter(subject__name=subject_query)
if membership_query:
qs = qs.filter(allowed_memberships__membership_type=membership_query)
Upvotes: 1