SpeedyH30
SpeedyH30

Reputation: 89

Select2 multiple select in django

I'm having a few issues with my Select2 drop-down. I originally only needed to select one item on my drop dpwn but I now need to have it as a manytomanyfield so Select2 seemed like the best option.

Here is my original code

JS

 $.get('/api/foos', function (response){
        $.each(response.foos, function(){
            $('#foo').append('<option>'+this+'</option>')
        })
    })

API

@require_GET
def Foos(request):
return JsonResponse({
    'foos':[x.foo_name for x in FOO.objects.all()]
})

{foos: ["shh", "fgdh", "fgb", "ghfs", "sfgh", "sfgh", "srth"]}

This worked nicely for single selection. now I am trying to convert to Select2, but I'm hitting a wall with it I cant seem to get any results into the drop down

$.get('/api/foos', function (response){
        $("#set_foo").select2({
            multiple:true,
            placeholder: "Select foos"});
            ('response.foos');
    })

and using the same api call

Upvotes: 0

Views: 1505

Answers (1)

SpeedyH30
SpeedyH30

Reputation: 89

fixed it

$.get('/api/locations', function (response){
        var data = (response.locations)
        console.log(data)
        $("#set_location").select2({
            data: data,
            multiple:true,
            tags:true,
            placeholder: "Select Locations",
        })  
    })

Upvotes: 0

Related Questions