0

im using django select2 for a chained single model select in register webpage. as i know django select2 expose an ajax endpoint that make all that is needed to search, paginate and select my cities. there is a way i can use this service that django-select2 creates automatic for using it in my app?

city = forms.ModelChoiceField(
    queryset=City.objects.all(),
    label=u"City",
    widget=ModelSelect2Widget(
        model=City,
        search_fields=['name__icontains'],
        dependent_fields={'country': 'state__country'},
        max_results=20,
        attrs={'class': 'form-control','width': '100%',},

    )
)

this is my city widget, and i have a a custom widget that did to test if i can expose it to everyone

class TitleSearchFieldMixin(object):
    search_fields = [
        'name__icontains',
        'pk__startswith'
    ]

class CitySelect2TagWidget(TitleSearchFieldMixin, ModelSelect2Widget):
    model = City

    def get_queryset(self):
        return self.model.objects.filter()

    def label_from_instance(self, obj):
        return force_text(obj.name).upper()


class CountrySelect2TagWidget(TitleSearchFieldMixin, ModelSelect2Widget):
    model = Country
    dependent_fields = {'country': 'country'}

    def get_queryset(self):
        return self.model.objects.filter()

    def label_from_instance(self, obj):
        return force_text(obj.name).upper()

thanks a lot for your answers

beren5000
  • 100
  • 10
  • 1
    There's nothing magical here, the "api endpoint" is nothing else than a view and url, so reading the doc and source code (plus eventually monitoring the requests/responses from your browser) should be enough to solve you problem. – bruno desthuilliers Mar 06 '19 at 15:25
  • thanks for your help bruno, as i see in the docs and the test that i made, django-select2 creates and endpoint, and search the field with a get parameter called field_id, but this field id change in every request you made in the page. as and endpoint i could not figure out how to get the field id, and this seems to be created in the request for the form, so, i guess i could not get this endpoint as a generic one for use it in my app, but thanks for your answer – beren5000 Mar 06 '19 at 23:58

0 Answers0