I have this simplified model and form:
class Books(models.Model):
name = models.CharField(max_length=500)
price = models.DecimalField(max_digits=6, decimal_places=2)
default = models.BooleanField(default=False)
def __str__(self):
return self.name
class BookForm(forms.Form):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['Field'] = forms.ModelChoiceField(queryset=None, empty_label=None, widget=forms.RadioSelect)
self.fields['Field'].queryset = Books.objects.all()
self.fields['Field'].initial = Books.objects.filter(default=True).first()
This will result in a RadioSelect-Form, like this:
(x) Book1
( ) Book2
( ) Book3
My problem is, how can I add the price in the RadioSelect form, that it's just visible. It should appear after the name of the book, ideally even in a different font, which I set over a bootstrap class (e.g "text-primary") (this is not mandatory)
(x) Book1 (10 €)
( ) Book2 (20 €)
( ) Book3 (30 €)
I know i can return the name and price in the model, like
class Books(models.Model):
name = models.CharField(max_length=500)
price = models.DecimalField(max_digits=6, decimal_places=2)
default = models.BooleanField(default=False)
def __str__(self):
return '%s (%s €)' % (self.value, str(self.price))
But because of other reasons, i can not do this. I just need to return the name. Are there other ways to do this?
I even read into django-crispy-forms, but couldnt find a solution.