Hi Alexander,

> Also, do you guys know a way of just getting the columns I need from database 
> and perhaps a straight return from the queryset?

Not sure if this helps, but by using `values`, you can restrict the
columns that are fetched from the db, so in your case, you can
probably simplify the code to something like:

    result = mymodel.objects.filter(isactive=True).order_by('name').values('id',
'name')

Documentation for `values` can be found at
https://docs.djangoproject.com/en/2.1/ref/models/querysets/#values

Unless you have a large number of columns on your model, though, I
don't see this improving performance a whole lot. Most importantly,
you should make sure you have an index on the `isactive` and the
`name` fields. If performance is still a concern, you could look into
using the caching framework and keep the items in some sort of cache
and hit the database only occasionally.

If the problem is  that you are returning a huge list of items and
generating a huge select box, the other option would be to use
something like an Autocomplete, that uses AJAX and fetches results as
the user types, though that is a more complex solution, out of the
scope of this answer.

Hope that helps and all the best!
-Sanjay

On Thu, Oct 18, 2018 at 10:42 AM Alexander Lamas
<alexander.g.la...@gmail.com> wrote:
>
> Hi all,
>
> I'm trying to find the possible best/faster way to build a lookup result set 
> to populate a HTML select tag (dropbox).
>
> I have done this, but I don't think is efficient and fast.
>
> class MyLookup(APIView):
>     def get(self, request, format=None):
>         result = []
>         for item in mymodel.objects.filter(isactive=True).order_by("name"):
>             result.append({"id": item.id, "name": item.name})
>         return Response(result)
>
>
> Do you guys know a better way?
>
> Also, do you guys know a way of just getting the columns I need from database 
> and perhaps a straight return from the queryset?
>
> Thank you very much!
>
> Regards,
> Alex
>
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Django REST framework" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to django-rest-framework+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-rest-framework+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to