Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extending Widgets #388

Open
paulchubatyy opened this issue Nov 19, 2022 · 0 comments
Open

Extending Widgets #388

paulchubatyy opened this issue Nov 19, 2022 · 0 comments

Comments

@paulchubatyy
Copy link
Contributor

paulchubatyy commented Nov 19, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant