Select request for data request from an api json?

1

More than a question, it is a query, I am currently with an api serving data, and I have arrived at the following situation, I have a user preferences table, where the configuration options of each user are stored, with json, I do I get a get and it returns all the data of that table, and then I have to filter that data with the user id that is logged in, my point is, there is another way to do this, that is, with mysql in the query I put the id of the user and only returned the data of that user, here I load the data of all users and then I have to filter. As I am inexperienced, I fear that if I have a record with more than a thousand data, and in the end I only need 1 of those data, I have to load all of that, and in the end it would affect the performance of my page.

It is already too late to back out with the api, I would like to know if there is a solution, or maybe I am exaggerating and this does not affect the performance.

Thanks in advance.

    
asked by Nestor Moran 08.10.2016 в 20:36
source

1 answer

1

As you do in MySQL, this is done in . It's much easier if you use models in your ViewSets

If you have a ViewSet for your model Usuario :

class UsuarioViewSet(viewsets.ModelViewSet):
    queryset = Usuario.objects.all()
    serializer_class = UsuarioSerializer
    permission_classes = [IsUsuarioAdminOrReadOnly]

You just have to add this to your routes:

from rest_framework import routers

router = routers.SimpleRouter()
router.register(r'usuarios', UsuariosViewSet)
urlpatterns = router.urls

And the routes are automatically generated:

  • User list: ^accounts/$ , it is called 'usuarios-list'
  • User detail: ^accounts/{pk}/$ and is called 'usuarios-retail'

And it has the most common verbs, POST , GET , PUT , UPDATE , DELETE and PATCH , as you have configured them.

I recommend that you consult the documentation of the project that is very complete and that you follow the rules of the guide How to make an example complete, minimum and verifiable when asking your questions.

    
answered by 09.10.2016 / 02:26
source