django-bootstrap5 icon indicating copy to clipboard operation
django-bootstrap5 copied to clipboard

Extending Widgets

Open paulchubatyy opened this issue 2 years ago • 0 comments

Hello team, first of all I would like to thank you for a great product.

I've got a custom widget for displaying stored credit cards in the store:

from django.forms import RadioSelect

class PaymentCardRadioSelect(RadioSelect):
    template_name = "forms/widgets/card_radio.html"
    option_template_name = "forms/widgets/card_radio_option.html"

When using my widget in the form:

from django import forms
from .widgets import PaymentCardRadioSelect

class FondyPaymentForm(forms.Form):
    card = forms.ChoiceField(
        choices=(), label=_("Card"),
        widget=PaymentCardRadioSelect()
    )

    def __init__(self, *args, **kwargs):
        cards = kwargs.pop("cards", ())
        super().__init__(*args, **kwargs)
        self.fields["card"].choices = cards

When I'm rendering the form, this line in renderers is being executed and it overrides the custom template that I have for card selection.

This happens because:

pw = PaymentCardRadioSelect()
assert isinstance(pw, RadioSelect) # evaluates to `True`

What is the recommended way of handling those scenarios?

Cheers, p

paulchubatyy avatar Nov 19 '22 03:11 paulchubatyy