I want to map radio buttons to given categories. One way is to do this with the help of a choice field. I don't know if this is a good approach.
I have seen many other articles and SO-posts but they did not point me to a direction from where I could see my mistake - so after many hours of research I wanted to ask you here. Thanks!
models.py
class Category(models.Model):
title = models.CharField(max_length=255, verbose_name="Kategorie")
...
class Post(models.Model):
title = models.CharField(max_length=150)
....
category = models.ForeignKey(Category, verbose_name="Kategorie",
forms.py
CAT_CHOICES= [
('Artikel', 'Artikel'),
('Videos', 'Videos'),
('Anderes', 'Anderes'),
]
class PostForm(forms.ModelForm):
category = forms.ChoiceField(widget=forms.RadioSelect, choices=CAT_CHOICES)
class Meta:
model = Post # is this throwing an error because I only handle the 'Post' model?
fields = [
'title',
...
'category',
]
views.py
def function_name(request):
...
if request.method == 'POST':
form = PostForm(request.POST)
if form.is_valid(): # error happens here
template.html:
<label>Artikel</label>
<input id="category0" type="radio" name="category" value="Artikel">
<label>Videos</label>
<input id="category1" type="radio" name="category" value="Videos">
<label>Anderes</label>
<input id="category2" type="radio" name="category" value="Anderes">
output:
ValueError at /
Cannot assign "'Artikel'": "Post.category" must be a "Category" instance.
What would be a good practice to handle this?
I am thankful for any advice.
SOLUTION
Even if I have spent a lot of time looking for my mistake I could not find it. With the two first answers I got pointed to the right direction. I have adjusted my template file and the form:
CAT_CHOICES= [
(1, 'Artikel'),
(2, 'Videos'),
(3, 'Anderes'),
]
class PostForm(forms.ModelForm):
category = forms.ModelChoiceField(queryset=Category.objects.all())
class Meta:
model = Post
fields = [
'title',
...
'category',
]
and
<label>Artikel</label>
<input id="category0" type="radio" name="category" value="1">
<label>Videos</label>
<input id="category1" type="radio" name="category" value="2">
<label>Anderes</label>
<input id="category2" type="radio" name="category" value="3">
Where the value is according to the id of the category. It would be interesting to know how to link the actual title and not the id.
Thank you!
UPDATE 2
As pointed by Matt the additional choices variable is not necessary. Thanks a lot it works!
