WEBVTT 00:00.120 --> 00:06.090 Hello everyone and welcome to this new lesson about the filtering system in January's framework by default 00:06.270 --> 00:13.290 the views that we have used so far to expose least of elements have always return whole query sits. 00:13.380 --> 00:19.800 Sometimes however you might want to get specific results out of the same least end point according to 00:19.800 --> 00:21.630 some predefined criteria. 00:21.660 --> 00:26.910 In this lesson we will learn how to improve our REST API by using django as frameworks filtering system 00:27.230 --> 00:30.390 and by personalizing the get equity set method of use. 00:30.630 --> 00:32.060 Let's get started. 00:32.130 --> 00:37.350 We are invisible to your code and as you can see I'm currently in the view's dot by file where we're 00:37.380 --> 00:41.040 now going to write most of the code for this lesson. 00:41.040 --> 00:47.120 Let's start off by modifying our profile start to view said classes by filtering their profile. 00:47.130 --> 00:53.910 Start to square is set in order to only get specific start to see instances created by the profiles 00:54.090 --> 00:55.430 was user user name. 00:55.440 --> 00:59.310 We are going to pass as you allow parameter in order to do so. 00:59.310 --> 01:08.250 We're now going to override the get query set method so therefore get query set which clearly accepts 01:08.620 --> 01:17.150 self-rule and we need to move that query set attribute from year to what method like so. 01:17.160 --> 01:19.970 So clearly this is the whole query set right. 01:19.990 --> 01:26.670 Well we're now going to check if they use it as passed a user name value via query parameter and in 01:26.670 --> 01:32.880 that case we're then going to feel that the query said accordingly so user name equals self. 01:32.880 --> 01:39.770 The request dot query parents dot getter and we want the user name. 01:39.930 --> 01:41.370 Otherwise none. 01:41.400 --> 01:46.710 So we want to get the value associated with the user name key within he bottoms. 01:46.770 --> 01:50.070 And of course we want to put this value within our user named variable. 01:50.160 --> 01:52.600 Otherwise non will be associated instead. 01:53.040 --> 01:59.660 So if a user name is not known not just to be as explicit as possible. 01:59.670 --> 02:08.240 If user name is a value other than non we can now feel that adequately said so quite said equals where 02:08.240 --> 02:19.170 he said that field that we use it profile user user name equals user name. 02:19.220 --> 02:24.370 If you're getting the quote is it this way it is only going to contain the profiles that we've seen 02:24.360 --> 02:32.750 since this was user profiles user user name is the same best as parameter we can then return adequate 02:32.790 --> 02:34.380 said whatever happens. 02:34.410 --> 02:40.010 So in case I use that name value is best we are going to feel that equity set accordingly. 02:40.020 --> 02:43.860 Otherwise we're just going to return the whole equity set pretty easy. 02:43.860 --> 02:47.590 We now need to make a small change in that you address that by fire. 02:47.610 --> 02:48.460 Precisely. 02:48.490 --> 02:55.020 Yea where we registry profiles that was view set and that's because our router class basically needs 02:55.590 --> 03:01.600 the query set attribute to create all the different endpoints for a view set and therefore in case you 03:01.590 --> 03:09.520 select this where we've just over written The get equity set method instead we need to pass basically 03:10.030 --> 03:10.830 equals. 03:11.020 --> 03:16.990 And then in this case we can just specify status and if you want a detailed explanation about why the 03:16.990 --> 03:23.230 base name parameter or the query set attributes are so important for the router class. 03:23.320 --> 03:28.840 I definitely suggest you check Django as frameworks documentation or simply take a look at the code 03:28.960 --> 03:30.430 of the class itself. 03:30.430 --> 03:31.820 So let's check it out now. 03:31.870 --> 03:40.420 Going to run the development server and let's go to Chrome and it is the EPA route. 03:40.430 --> 03:43.080 So let's look in for example there is rest. 03:43.130 --> 03:43.580 Tester 03:46.390 --> 03:53.930 like so let's go to status and we've got our to start to see this created by admin so let's create a 03:53.930 --> 03:57.710 new one first. 03:57.950 --> 04:06.840 Hello world post so as you can see we now get to start to see instances created by the ad the mean profile 04:07.010 --> 04:09.910 and one clearly created by arrest test. 04:10.020 --> 04:15.570 Let's say we only wanted this stuff to see instances created by youd mean because of the way we set 04:15.570 --> 04:17.250 up the get query set method. 04:17.250 --> 04:23.280 We can now pass ad mean as parameter to our view set in order to filter the query set accordingly so 04:23.280 --> 04:24.050 we can do. 04:24.060 --> 04:27.700 Question mark is that name equals admin. 04:28.080 --> 04:36.180 So that point is basically the same but this time we are passing admin as value of the user name parameter 04:36.360 --> 04:41.580 and as you can see we now only get the two statutory instances created by admin. 04:41.610 --> 04:48.270 Let's not get only D start to see instances created by rest testing and clearly as you can see in this 04:48.270 --> 04:51.720 case we only get this that was the instance we've just created. 04:51.720 --> 04:56.940 Let us know so we can use one of the classes of Django as frameworks ability in system to filter their 04:57.110 --> 05:03.130 face and point in order to only get profile instances from a specific city. 05:03.340 --> 05:12.650 And first of all let's go to the admin interface to actually set a different city for every instance. 05:12.720 --> 05:28.280 So profiles and we got one 2 City for example New York save Yeah we can set Rome and we know that admin 05:28.520 --> 05:29.590 is from Testament. 05:29.690 --> 05:35.570 So let's go back to visit us and your code now specifically to the view stood by file and let's import 05:35.600 --> 05:37.180 a search field there. 05:37.190 --> 05:45.140 So from rest framework that filters import search filter. 05:45.160 --> 05:54.980 Now in profile view said we can add another attribute filter against equals we can use the least search 05:55.970 --> 06:01.520 filter and then we need to pass another attribute which is search fields. 06:01.550 --> 06:09.380 And in this case we want to search as we said using the s.t. field that we can pass as drink. 06:09.470 --> 06:12.530 Let's now restart our development server. 06:12.560 --> 06:15.470 Let's go back to Chrome right away. 06:15.470 --> 06:22.470 So slash API we can then go to profiles. 06:22.630 --> 06:31.020 And so yeah this time we need to pass the search parameter so question mark search and then devalue 06:31.020 --> 06:37.150 value for example test Lent we only get the admin instance. 06:37.150 --> 06:45.160 Or for example row how we get the rest to test search filter is also really cool because we just need 06:45.160 --> 06:50.050 to pass partial parameters like for example new for New York. 06:50.050 --> 06:54.070 And as you can see we still get the results pretty cool. 06:54.070 --> 06:59.530 Not only that but as you can see the browser built API is adapted to the new filtering system we've 06:59.530 --> 07:07.330 set up as you see we now go to filters back down that we can use to perform our search search and you 07:07.330 --> 07:08.960 are the are the results. 07:09.050 --> 07:09.320 Okay. 07:09.340 --> 07:13.160 So that was it for this election about the filtering system in January's framework. 07:13.270 --> 07:17.320 In the next lecture we're going to talk about another very important topic. 07:17.320 --> 07:19.390 Automated tests in January's framework.