#Dynamic Formset + django-select2

4 messages · Page 1 of 1 (latest)

gilded crystal
#

Hello !

I use django-select2 for complex lookup forms and it works very well in all the forms where I add it.
Besides, I am using the attached JS to dynamically add/remove rows in a formset; it works very well for most of my formset use cases.
Unfortunately, when adding/removing a row where one of the fields is a select2, the related select2 js is not populated and I end up with a basic select field. I have a very limited JS knowledge 😅 and I only managed to get this far after scraping various forums - do you see something I need to tweak in the JS in order to call the .select2() at the right moment ? If you have materials where I am able to learn more on this topic I am very interested.

Thanks in advance !

class ContactS2Widget(s2forms.Select2Widget):
    search_fields = [
        "nomcomplet__icontains",
        "mail__icontains",
        "mail_perso__icontains",
        "adresse__icontains",
        "ville__icontains",
        "note__icontains",
    ]

class ActeursForm(forms.ModelForm):
    class Meta:
        model = Acteurs
        exclude = ()
        widgets = {
            'contact': ContactS2Widget(attrs={"data-theme": "bootstrap4", "class": "form-select"}),
            'role': forms.widgets.Select(attrs={"class": "form-select"}),
        }

wise temple
gilded crystal
#

Thanks a lot !! I’ll have a look - indeed it looks very similar.

gilded crystal
#

Hi Jacob, I played with the django-formset plugin, it's very well documented. I managed to display and work with single forms, however I face a problem with collections of related models.

I have the following URL, View and Forms:


urlpatterns = [
    path('test/<int:pk>', views.OpportuniteCollectionView.as_view(), name='test'),
]

class OpportuniteCollectionView(FormCollectionView):
    collection_class = OpportuniteCollection
    template_name = 'z_test.html'
    success_url = reverse_lazy('home')


class ActeursForm(forms.ModelForm):
    default_renderer = FormRenderer()
    class Meta:
        model = Acteurs
        exclude = ('created_by', 'updated_by')
        widgets = {'role': forms.widgets.Select(attrs={"class": "form-select"}),}

class ActeursCollection(FormCollection):
    min_siblings = 1
    acteurs = ActeursForm()
    legend = 'acteurs'
    add_label = 'add acteurs'
    related_field = 'opportunite'

    def retrieve_instance(self, data):
        if data := data.get('acteurs'):
            try:
                return self.instance.acteurs_set.get(id=data.get('id') or 0)
            except (AttributeError, Acteurs.DoesNotExist, ValueError):
                return Acteurs(name=data.get('name'), department=self.instance)  # TODO: fix this

class OpportuniteCollection(FormCollection):
    opportunite = OpportuniteForm()
    acteurs = ActeursCollection()

{% extends 'base.html' %}
{% load static %}
{% load formsetify %}

{% block content %}
<br>
<div class="container text-left">
    <django-formset endpoint="{{ request.path }}" csrf-token="{{ csrf_token }}">
        {% render_form form %}
    </django-formset>
{% endblock %}

If I use the {{ form }} tag in the template, nothing shows, and if I use {% render_form form %} it gives me an error:
Must be applied to a Form object inheriting from 'django.forms.BaseForm'.