Django (django-rest-framework) with Bootstrap Autocomplete. Filed Nameing

I want to autocomplate a field.

But i can't figure out how i can accomplish properly because i don't know yet what is the industry standard.

Seems to be the Bootstrap Autocomplete expect an particular field naming like this. I think this is hardcoded. (the "text" at least):

[
    { "value": 1, "text": "Google Cloud Platform" },
    { "value": 2, "text": "Amazon AWS" },
    { "value": 3, "text": "Docker" },
    { "value": 4, "text": "Digital Ocean" }
]

How i can alter/mask the filed names from ['id','provider'] to ['value','text'] in Django side?

Let say i have:

#modell
class CloudProviders(models.Model):
    provider = models.CharField(max_length=255, blank=True, null=True)
    #id come by default
    ...

#Serializer
class CloudProvidersAutocompleteSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = CloudProviders
        fields = ['id','provider']
        
#view
class  CloudProvidersAutocomplete(viewsets.ModelViewSet):
    model = CloudProviders
    serializer_class = CloudProvidersAutocompleteSerializer
    def get_queryset(self):
        query= self.request.query_params.get('search')
        if query :
            data = CloudProviders.objects.filter(provider__contains=query)
            return  data

Shouldn't live open this question anymore. The key idea was to add an extra property to modell what provide the required informations.

modell

class CloudProviders(models.Model):
    provider = models.CharField(max_length=255, blank=True, null=True)
    #id come by default

    def _getProvider (self):
    return self.provider 

    text = property(_getProvider )

    ...
Back to Top