Hello everyone and welcome to this new lesson about the filtering system in January's framework by default
the views that we have used so far to expose least of elements have always return whole query sits.
Sometimes however you might want to get specific results out of the same least end point according to
some predefined criteria.
In this lesson we will learn how to improve our REST API by using django as frameworks filtering system
and by personalizing the get equity set method of use.
Let's get started.
We are invisible to your code and as you can see I'm currently in the view's dot by file where we're
now going to write most of the code for this lesson.
Let's start off by modifying our profile start to view said classes by filtering their profile.
Start to square is set in order to only get specific start to see instances created by the profiles
was user user name.
We are going to pass as you allow parameter in order to do so.
We're now going to override the get query set method so therefore get query set which clearly accepts
self-rule and we need to move that query set attribute from year to what method like so.
So clearly this is the whole query set right.
Well we're now going to check if they use it as passed a user name value via query parameter and in
that case we're then going to feel that the query said accordingly so user name equals self.
The request dot query parents dot getter and we want the user name.
Otherwise none.
So we want to get the value associated with the user name key within he bottoms.
And of course we want to put this value within our user named variable.
Otherwise non will be associated instead.
So if a user name is not known not just to be as explicit as possible.
If user name is a value other than non we can now feel that adequately said so quite said equals where
he said that field that we use it profile user user name equals user name.
If you're getting the quote is it this way it is only going to contain the profiles that we've seen
since this was user profiles user user name is the same best as parameter we can then return adequate
said whatever happens.
So in case I use that name value is best we are going to feel that equity set accordingly.
Otherwise we're just going to return the whole equity set pretty easy.
We now need to make a small change in that you address that by fire.
Precisely.
Yea where we registry profiles that was view set and that's because our router class basically needs
the query set attribute to create all the different endpoints for a view set and therefore in case you
select this where we've just over written The get equity set method instead we need to pass basically
equals.
And then in this case we can just specify status and if you want a detailed explanation about why the
base name parameter or the query set attributes are so important for the router class.
I definitely suggest you check Django as frameworks documentation or simply take a look at the code
of the class itself.
So let's check it out now.
Going to run the development server and let's go to Chrome and it is the EPA route.
So let's look in for example there is rest.
Tester
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
new one first.
Hello world post so as you can see we now get to start to see instances created by the ad the mean profile
and one clearly created by arrest test.
Let's say we only wanted this stuff to see instances created by youd mean because of the way we set
up the get query set method.
We can now pass ad mean as parameter to our view set in order to filter the query set accordingly so
we can do.
Question mark is that name equals admin.
So that point is basically the same but this time we are passing admin as value of the user name parameter
and as you can see we now only get the two statutory instances created by admin.
Let's not get only D start to see instances created by rest testing and clearly as you can see in this
case we only get this that was the instance we've just created.
Let us know so we can use one of the classes of Django as frameworks ability in system to filter their
face and point in order to only get profile instances from a specific city.
And first of all let's go to the admin interface to actually set a different city for every instance.
So profiles and we got one 2 City for example New York save Yeah we can set Rome and we know that admin
is from Testament.
So let's go back to visit us and your code now specifically to the view stood by file and let's import
a search field there.
So from rest framework that filters import search filter.
Now in profile view said we can add another attribute filter against equals we can use the least search
filter and then we need to pass another attribute which is search fields.
And in this case we want to search as we said using the s.t. field that we can pass as drink.
Let's now restart our development server.
Let's go back to Chrome right away.
So slash API we can then go to profiles.
And so yeah this time we need to pass the search parameter so question mark search and then devalue
value for example test Lent we only get the admin instance.
Or for example row how we get the rest to test search filter is also really cool because we just need
to pass partial parameters like for example new for New York.
And as you can see we still get the results pretty cool.
Not only that but as you can see the browser built API is adapted to the new filtering system we've
set up as you see we now go to filters back down that we can use to perform our search search and you
are the are the results.
Okay.
So that was it for this election about the filtering system in January's framework.
In the next lecture we're going to talk about another very important topic.
Automated tests in January's framework.