Discussion:
Django's fastest way to create a lookup
Alexander Lamas
2018-10-18 05:12:56 UTC
Permalink
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+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Sanjay Bhangar
2018-10-18 08:01:47 UTC
Permalink
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
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.
result = []
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.
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+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Alexander Lamas
2018-10-18 08:21:53 UTC
Permalink
Hi Sanjay,

Thanks for your reply.
It works perfectly.

Thank you!

Regards,
Alex
Post by Alexander Lamas
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.
result = []
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+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Arbaz Hundekar
2018-10-22 03:49:58 UTC
Permalink
Hi Alexander,
Always remember one thing try to do most operations on django queries than
python loops. Because SQL or ORM works faster.
Answer mentioned by sanjay is faster.
Post by Alexander Lamas
Hi Sanjay,
Thanks for your reply.
It works perfectly.
Thank you!
Regards,
Alex
Post by Alexander Lamas
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.
result = []
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
For more options, visit https://groups.google.com/d/optout.
--
Warm Regards,

Arbaz
--
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+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...