applegrew / django-select2

This is a Django integration for Select2
MIT License
710 stars 316 forks source link

Implementation of Select2Widget with formsets. #606

Closed jpm92 closed 4 years ago

jpm92 commented 4 years ago

Hello, I'm trying to implement a django formset of Select2Widgets with a dropdown search bar. I've worked with django-select2 in the past with success when using a regular django form, but it seems to not work the same way when the formset factory is applied. Is this a bug? Am I missing something? I couldn't find anything in the docs (maybe I'm just blind after so many hours...)

Working snippet of the website:

#forms.py
class PedidoFungibleForm(ModelForm):
    class Meta:
        model = PedidoFungible
        fields = ['producto', 'unidades']
        widgets = {
            'producto': Select2Widget(
                {'data-language': 'es',
                 'data-placeholder': _('Busque fungibles'),
                 'data-width': '100%',
                 }
            )
        }

Not working snippet:

forms.py
class FormularioSolicitud(ModelForm):
    class Meta:
        model = Cesta
        fields = ['producto', 'unidades']
        widgets = {
            'producto': Select2Widget(
                {'data-language': 'es',
                 'data-placeholder': _('Busque productos'),
                 'data-width': '100%',
                 }
            )
        }
views.py
@login_required
def AñadirSolicitud(request):
    """ Hemos de crear las instancias de "Cesta" una a una con su respectivo
    enlace a la solicitud que se está creando. """
    SolicitudFormset = formset_factory(FormularioSolicitud, extra=5)
    if request.method == "POST":
       blabla
    else:
        formset = SolicitudFormset()
        return render(request, "nueva_solicitud.html", {'formset': formset})

This is the result of the first example I implemented long ago. imagen

I see there is this PR (#599), but it's still unreviewed and I'm not even sure what's its purpose.

jpm92 commented 4 years ago

Well, I thought my issue was the same as in #560: not naming the form "form" in the context. But I renamed it and it didn't work either.

codingjoe commented 4 years ago

Hi @jpm92 is that formset inside Django admin? If not, are you otherwise dynamically adding rows? Best Joe

jpm92 commented 4 years ago

Hi @jpm92 is that formset inside Django admin? If not, are you otherwise dynamically adding rows? Best Joe

Hi Joe, I actually don't know how or why, I still have to review my commit, but I was working with something else and it just started working. I think it had to do with the position of the {% extra css %} block where the formset.css was loaded. Anyway, sorry for the inconvenience. I will edit and close this issue when I find what I changed in case it could be useful to someone else.

Thanks!

codingjoe commented 4 years ago

This repo has moved, please address your issue at https://github.com/codingjoe/django-select2 Thanks!

gusarg81 commented 4 years ago

Hi, I am want to address and issue/question/doubt but i the new repo I don't see the issue tab.

codingjoe commented 4 years ago

@gusarg81 haha, good catch. Seems GH disables those by default for forks 👍 🥇