Django Conditional Expressions / Case() and When() objects

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to look at conditional expressions in Django and we're going to see how these allow us to perform if and else logic within our database statements such as filtering aggregating and annotating so a conditional expression will allow us to evaluate some conditions for each row in a table and then return the matching result expression based on those conditions and we're going to learn about two objects that we can use within the Django orm and that's the when object and these control the logic of the conditions and we're also going to learn about the case object and these encapsulate the conditions for the query so let's get started I have vs code open here and we've extracted the Italian restaurant type here and we're going to use that within a query and that's going to be a conditional expression query that we're going to build up with the Django orm now to start very simply we just want to take every single restaurant that we have in our database and we want to annotate that restaurant with whether or not it is an Italian restaurant so if we go to models.pi and we go to the restaurant model you can can see we have all of these different types of restaurants that are defined in a type choices class and for the restaurant type field we are adding those choices here at the right hand side and that means that a restaurant can be any one of these restaurants what we're going to do is annotate every single restaurant that we're getting back from the database with a simple Boolean output field that tells us whether or not the restaurant is Italian now we can use the case and when objects to do this we can use these conditional Expressions so let's see how to do that now so we're going to get back a set of restaurants we're going to store that in a variable called restaurant and it's going to be restaurant.objects dot annotate and we're going to annotate the query set and we're going to add a Boolean field let's give that field a name of is Italian and we're going to set that equal to a case expression a conditional expression and Django now in order to do this we're going to use the case object which we need to import at the top here from the Django models module so let's import that and we're also going to use another object called when now as we mentioned in the intro the case object here this encapsulates the condition and we use the when object to actually Define the logic of the condition and that's going to be this F else logic that we can use within the orm statement so let's see exactly how to do this in the case object we're going to define a when object and we're going to use the restaurant type field and we're going to check if that is equal to this Italian type that we've pulled out on Lane 8. now this statement will either evaluate to true or false if it evaluates to true then we can pass in a keyword argument of then and this will Define the value that's coming back from the database if this condition is true so if the restaurant type is Italian we're going to return true in that case now when you're using these case objects you can also Define a default that's what we're going to do at the end here we're going to define a default value of false so whenever we have a restaurant whose restaurant type is not equal to Italian the default value is going to be fired in that case and that's going to be false otherwise we're going to get back true here now we can Define as many when objects as we want within the case expression and we're going to see examples of that very soon but once we've done this annotation we will have a field called is Italian on our restaurants so what we can do here is print these out I'm going to use the print statement here and we're going to print out restaurants dot filter and we're going to filter down these restaurants to the ones where the is Italian field is equal to true so let's run this script on the terminal with the python manage.pi run script command and the name of the script is orm script this should now return the Italian restaurant stores and you can see the restaurants that are printed out here these are all Italian restaurants that are coming back from the database so let's go over what's going on here what we're doing is we're pulling out the Italian type and we're annotating every single restaurant in the database with a field called is Italian and that annotated value is the result of a conditional expression which we Define with the case object in Django and inside that case object we're passing these when object works that Define the logic of the conditions so in this case we check if the restaurant type is Italian if it is we're going to annotate that value with true and we have no further when objects so the default will then fire if it's not an Italian restaurant and set this annotation to false we then print these out to the terminal and we can use that annotated field in order to get back the values that we want so this is how to use in a very simple way a conditional expression in Django to check a particular condition in the database and Define a value in the case that that is true and very quickly before we move on let's check the connection dot queries by uncommenting that and we can see the SQL that is executed in the database in order to generate this output that we see here so if we look at the SQL statement you can see the select statement is selecting all of these different fields but we have this expression here the case when expression and within that we are checking the restaurant type if it is equal to Italian then we are returning one otherwise with this else statement we are returning 0 and then we end that case expression and we Alias the resulting value with the is Italian value that we have put into the annotate method as you can see up here so that's one example of how to use these conditional expressions in Django let's remove this code here and we're going to bring in some new code instead let's say that we wanted to annotate each restaurant with a Boolean that indicates whether or not it had more than eight sales in the database so let's define a statement here it's going to be restaurant.objects dot annotate and let's start by just annotating with a value number of sales which I'm going to set equal to the current aggregation function over the sales and if we look at models.pi we have the sale model that has a foreign key to restaurants and the related name for that foreign key is sales so when we are querying over the restaurants here we can look at that reverse relationship and get back all of the sales by using this field and then we can aggregate using this count function in this case over all of those sales so so that will annotate each restaurant with its number of sales in the database and we can get just those values back using the dot values function so we're going to print out this statement and see what kind of values we're getting back for these restaurants so let's execute this script and we're going to see the output that we're getting at the bottom here we have this query set coming back it just contains this new End Sales value and you can see that there's values from three up to 10 here and in fact we have one restaurant with 11 sale so the values are between 3 and 11. let's now add another annotated field here called as popular and we're going to set that to true if the restaurant has more than eight sales in the database so let's go back to our query here now what I'm going to do is remove this print statement and store the results of this query in a variable called restaurants and we can also remove the call to dot values we're just going to annotate each restaurant with the number of sales once we've done that we can then perform a further query so it's going to be restaurants here we're going to read in the variable I'm going to set that equal to restaurants dot annotate and the annotated field this theme is going to be called as popular and again we're going to set that to a conditional expression here so let's define our when statement here for the logic of this expression we're going to check if the number of sales is greater than 8 and if that is true what we're going to do is we're going to return the Boolean value true here and we can Define again a default of false in this case so this is a little bit more complex than the previous example we're annotating every restaurant with a value indicating the number of sales and then we're performing a further annotation here where we use a conditional expression over the previously annotated value to check whether it's greater than a particular number and set our return value a conditional value based on whether that's true or false so what I'm going to do now is I'm going to print out another statement here we're going to print out restaurants.values and let's take the two annotated values here we're going to print out the number of sales and we also want to print out the new value which is as popular so let's again rerun the script at the bottom I'm going to bring this up here and you can see that for restaurants whose number of sales is less than or equal to eight there is popular field is set to false but if it's greater than 8 in this case it's nine it's set to true so we are annotating every single restaurant that we're getting back from our query set here with the number of sales that that restaurant has in the related table of sales and then we're taking that annotated value and we're performing a further annotation to check whether or not it's a popular restaurant and we Define that boundary at eight and just like before we can then use the annotated value and a filter expression so let's go down here and what we're going to print out here is restaurants.filter and we're going to only get back the restaurants where is popular is equal to true so let's run the query at the bottom and we can see the output there and those are the restaurants that have more than eight sales and are therefore deemed as popular restaurants in this system now as I said earlier we can actually pass multiple conditions to the case object that encapsulates these conditional Expressions so what I'm going to do is remove all of this code here we don't need any of that anymore we're going to start by defining a new problem here and we're going to write these conditional Expressions to solve the problem so I'm going to write a comment at the top here and these are the conditions that we're going to look for here let's imagine that what we want to do is find all restaurants who have a good average rating in this case a rating of greater than 3.5 but we also want to check that the restaurant has more than one rating in the database so why might you want to do this in this imaginary system well you might have one person who has rated the restaurant five out of five that might be the owner of that restaurant but that doesn't necessarily mean that the restaurant is good it's just one single person who's given it that rating so we might want to add up conditional check here to make sure that more than one or more than x number of ratings have been added for that restaurant so let's start by annotating each restaurant with the average rating and also the total number of ratings for that restaurant so let's Define a variable called restaurants and we're going to use the restaurant.objects dot annotate function and we're going to annotate each restaurant with two new Fields first of all the average rating and we use the average aggregation function and for each restaurant we have the ability to look at the ratings that's a reverse foreign key if we go to models.pi and we look at the rating model there is a foreign key from that model to the restaurant and that has this related name at the right hand side of ratings so we can use that related name here and get the average over the ratings but we also need to follow the reference to the rating and access its actual numerical field called rating and that refers to this field here which is a positive small integer field so just to make that clear we're looking at the restaurant model we're following the foreign key to all of the associated ratings and we're looking at the rating field here for each one of those and then calculating the average of those ratings and as well as the average we also want to get the count the number of ratings that have been associated with that restaurant so we'll add another anesthetic field called num ratings and we're going to set that equal to the count aggregation function and again we're going to look at the ratings that we have associated with this restaurant and if we want to get all the unique ratings we can just look at the primary key of those Associated ratings so that's the restaurants let's print out the values of those annotated Fields using the dot values function and the fields are called average and num ratings so we're going to print those out and look at the terminal at the bottom so let's see the output that we're getting here we have average ratings for example five from four ratings 1.5 from 2 ratings and so on and we also have a few values here we have one equal to none because there are no ratings for that particular restaurant now if you don't want none values in your aggregation functions check out the last video that we did on hand null values and the coalesce function in Django but for now we're going to keep going with this example we have the restaurants and we have annotated each one with its average rating as well as the number of ratings that it has in the database what we now want to do is perform another annotation and we're going to add a field that's conditional to each restaurant and that value is going to be true for the condition if the rating or the average rating is greater than 3.5 and the restaurant has more than one rating so let's remove the print call here to restaurant.values and what we're going to do is we're going to use the restaurants that we defined above and we're going to use another annotation here by calling the annotate function and our annotation this time is going to be called highly rated and we're going to set that equal to another case expression in Django so this time we're going to create a win object but we're actually going to pass two different conditions to that object and you can see from the intellisense here on Visual Studio code that it can take as many conditions as you want and then it has this then keyword argument that we set after those conditions so what we're going to do is we're going to specify the conditions here we want the average to be greater than 3.5 so if it's a highly rated restaurant it must have an average rating of greater than 3.5 and let's now pass that second condition we're going to look at the number of ratings which is one of the other annotated fields and we're going to check that we have more than one rating in the database for the restaurant and if both of those conditions evaluate to true we can set the returned value to true as well by using the then keyword argument so that's our when condition and again we can specify a default of false so if these conditions don't evaluate to true the highly rated annotation is going to be set to false otherwise we'll set it to true and once we've done that I'm going to use a print statement here and we're going to look at the restaurants that we have and we're going to use the dot filter function in Django and we're going to look at that annotated field of highly rated and we're gonna look only for those restaurants where that value is set to true so let's execute the query on the terminal below and you can see we get back these restaurants now these restaurants Must ALL match the two conditions that we specified the average rating must be greater than 3.5 and the restaurants must have more than one rating in the database and in that case we get back the highly rated annotation set to true and then we can filter that as we're doing below here so this example has highlighted that we can pass multiple conditions into the when object in Django and the then statement here which will return a particular value will only be executed or it will only set the value to that if all of those conditions evaluate to true now what we can also do as I said earlier is we can pass multiple when objects into this case object here so let's see an example of that now rather than just checking if it's a highly rated restaurant we're going to filter the restaurants into different buckets here and let's call this rating bucket in this case and what I'm going to do is I'm going to change the code that's in the when object here so we're going to create three different buckets here in our application a restaurant can either be highly rated it can have an average rating or it can have a bad rating so let's start with the bucket for highly rated restaurants we can look at the average annotation that we got above and we can check if it's greater than 3.5 and if that's true we can set the then value and instead of just a billion as we've been doing up until now I'm going to set a raw value here by importing the value object from Django's models and then we can go back down to this statement and we can instantiate our value object and we can pass in a string to that and the string I'm going to pass in is highly rated so that's the bucket for highly rated restaurants they will have an average rating that's greater than 3.5 I'm going to copy this line down below and we're going to define a second when object here and this is for restaurants with an average rating so I'm going to change change the value of the then expression to average rating and what we need to do now is Define the conditions for which a restaurant will have an average rating so again we're going to look at the average field but instead of greater than I'm going to use the range lookup here and we're going to look for restaurants whose average rating is between two values and the values of 2.5 and 3.5 and the range lookup in Django we've covered that before but I will mention now that that is inclusive so it will include all restaurants who have an average rating of 2.5 and also any restaurants that have an average rating of 3.5 so this is inclusive so we're not going to get any overlap between this and the one above where we're only looking for restaurants that have an average rating that's greater than 3.5 so when a rating bucket conditional expression here we have two conditions to win objects and also at default let's add a third when object just below here and this is for restaurants who have a bad rating so I'm going to change the value that's returned here to bad rating and finally we can change the lookup as well we're going to look for restaurants who have a rating on average rating of less than 2.5 and any restaurants that match that condition should have this annotated field return a value of bad rating so hope that makes sense we can then use this annotation within any further expressions in the orm so I'm going to use the restaurant stock filter statement that we have here I'm going to pass in that new annotated field here we're going to look for restaurants whose rating bucket is equal to highly rated now we're using some magic strings here you might want to factor these into an enum or a similar data structure but we're just going to do this for now let's execute this query and we're going to see what restaurants are highly rated and we're getting this error at the bottom here and that is that we have an expression that contains mixed types the car field and the Boolean field so what we have here we have a problem and that's because I've left the default value in this expression we don't need that because these can conditions will cover all possibilities we either have a restaurant whose average is less than 2.5 or we have restaurants whose average is greater than 3.5 or we have restaurants that are somewhere in between those values we don't need the default and the problem there was we were setting the default to false whereas all of the others and these wind conditions these were returning strings or car fields and we shouldn't mix those types when we're using these conditional Expressions so let's now try and re-execute this query now that we've removed the default and you can see we're getting back all of these restaurants that have the high rating or the average rating of greater than 3.5 if we look at the restaurants who have a bad rating we can look at this value here and paste that into this rating bucket filter and then when we re-execute this we're going to get back a different set of restaurants this time and these are the restaurants whose average rating should be below 2.5 let's now move on and show another example now we can use Q objects which we saw in a previous video Within These when Expressions so again I'm just going to remove most of this code for now and I've left a comment now at the top here for what we now want to achieve we want to assign a continent to each restaurant in the database and the continent is going to be based on the restaurant type field so let's scroll up to the restaurant model we have these different restaurant types for example Indian Chinese Italian and so on what we want to do here is we want to assign a continent to each of those based on the type a bit of a contrived example but it's going to show quite a useful concept here in Django how we can achieve this so for example Indian and Chinese would have the continent of Asia and Italian and Greek would have Europe so let's go back to the script and we're going to write the code for this just now again we're going to use the restaurant's annotate method and The annotation here that we're going to add is for the continent and we're going to set that to a conditional expression using the case object and we're going to Define multiple conditions here so the first one is going to be aware regression that's going to check if the restaurant is an Italian restaurant or a Greek restaurant and if it's one of those it's going to assign the value of Europe to this continent so what I'm going to do in order to do that is I'm going to import the Q object and that's because we need to perform an or query here so let's go back to the Wayne object and we're going to define a couple of Q Expressions within that we're going to look at the restaurant type and we're going to check if that is equal to Italian now in order to check this just to make this a little bit easier I'm going to get a reference to these type choices within the script here so let's paste an expression in here where we have a variable called types let's set to that type choices class we can then reference types like this which is going to save me a bit of typing and for European restaurants we can look for the Italian type and then and this is the reason that we're going to use the Q objects we can perform the or query here and again we can look at restaurant type and this time we can check if it's equal to types dot Greek so if the restaurant's one of those two types which can then use the then keyword argument and set the value equal to this value object and pass in Europe here so in order to make this a bit clearer I'm going to close the sidebar on vs code so we can see that we are checking if the restaurant is Italian or if it's Greek and if it's true if that evaluates the true we're setting the return value here to Europe so let's now add another when expression below we're going to check for Indian restaurants here and Chinese restaurants in the second when expression and if one of those two evaluates to true what we're going to do is we're going to set the value to Asia and if we go back to models.pi as well as these types we also have Mexican which we're going to look for now so let's copy this when expression down below we don't need an or statement here because we're just checking for a single value so we can actually remove the Q object from this expression and we're just going to check if it's equal to Mexican and if that's true the continent is going to be North America and we have a couple of other types in this type choices class we have fast food and other now these don't have a continent associated with them so what I'm going to do is Define a default value here I'm going to set that to the value of not available so I hope that makes sense we have this statement here looking at the restaurants and annotating each restaurant with a continent and we're using multiple when objects in Django within this conditional expression to set the value to Europe Asia North America or not available so we can then use this annotation in the Django filter statement so what I'm going to do is print out again restaurants.filter and let's say that we wanted to get all restaurants who had an Asian Cuisine we can look at the continent and set that equal to Asia and then when we execute the script at the bottom that should now return to us the Asian restaurants so we have these Indian restaurants here and we also have Chinese restaurants so that seems to have worked and if we look at European restaurants and execute this script again we should get back this time the European restaurants and these are just the Italian ones I don't think there are any Greek restaurants in the database this shows how we can annotate a particular model in Django and the rows that are returned from the database with a useful value that uses conditional Expressions to check a particular value and then returns a value based on whether those expressions are true or false now what I'm going to do now is rewrite this query to something that is much cleaner to read as a developer and that's very important and it's one of the benefits of using these Q objects in Django so in order to do that I'm going to copy these Q Expressions out of the when object and I'm going to define a few variables just above this expression now to start with I'm going to Define European as a variable here and we're going to set that equal to this Q expression where we're checking if it's Italian or if it's Greek and then we can pass that in here and that is going to be referenced as European and that's much cleaner to read in this case in my opinion and we can do the same for the other values as well so let's pull out the Asian values and we can store that in a variable called Asian and again we can reference that below and we can do the same for the North American restaurant types here in this case we don't have a Q object we're just looking at the restaurant type and checking if it's Mexican I'm going to cut that out of there and at the top we can Define that variable of North American and we can set that equal to a q object and paste that condition in there and then we can reference again North American within the when object and I think that this statement is a bit cleaner than it was before and that's because we've extracted the filters into dedicated objects Q objects and then we reference those within the Wayne objects and that's much cleaner to read in my opinion now I want to finish with a more complicated example so I'm going to remove all of this code that we have here what we want to try and do using when objects is aggregate the total sales over a 10-day period for each restaurant starting from the first sale up until the last so in other words we want to aggregate all sales from the first to the 10th of the month and then we also want to do the same for the 11th until the 20th and so on so we're aggregating all of the sales in the database over 10 day Windows let's see how we can achieve that using these when Expressions so what I'm going to do is Define two variables here first sale and last sale and I'm going to import the Min and Max aggregations from Django at the top here and what we're doing just below here is we're getting the date of the first sale by looking at the minimum over the date time column in the sale table and we're doing the same for the date time column and we're looking at the maximum to get the last sale date so we have the first sale and the last sale the dates for lows and now we want to calculate aggregations over the sales in 10 day Windows between the for sale up until the last sale so let's clear the terminal I'm going to make that a little bit smaller where we write this code so what we're going to do now is we're going to generate a list of dates that are each 10 days apart and we're going to start from the date of the first sale so what I'm going to do at the top is I'm actually going to import Python's it up tools module just to show this example people and aware that we might not have seen before and I'm going to create an empty list for the dates that we have in the database and I'm going to use the etotools.count function to create a counter essentially that returns an iterator that we can progress infinitely by calling the next function so to start with it'll be equal to 0 and then it will be set to 1 when we call next again and then two and so on what we're now going to do is we're going to try and populate this list of dates with a set of dates that starts at the date of the first sale and then progresses in 10 day increments after that so in order to populate that list I'm going to create a while loop here and we're going to create a variable called DT using Python's walrus operator and we're going to look at the first cell that we have here and we're going to add to that our timezone.time Delta and what we're going to add is the time Delta here is we're going to set this equal to a number of days and that's going to be 10 multiplied by calling the next function over that count and we're going to continue this look while that resulting value is less than or equal to the date of the last sale so I'm just going to write the logic it's a simple a pen statement here where we're appending the dates that we get back from this expression here to the list of dates that we have above so what's happening here is we're setting a variable called DT and that's going to be equal to the date of the first sale plus a Time Delta of 10 multiplied by whatever the count variable is at the given time and the first time that that while loop runs the count variable will be set to zero when we call next so it's going to evaluate to zero here and just add the date of the first sale to that list and the second time that the loop runs the value after calling next is going to be one so 10 multiplied by 1 will evaluate to 10 so we're adding 10 days to the date of the first sale and then the next thing that the loop runs the value of count is going to be 2 after calling next and 10 multiplied by 2 is going to be 20. so each time that this it runs we are adding 10 days to the date of the first sale and then it runs again and we add another 10 days so we're generating these 10 day windows and I think the easiest way to see that is to print these dates that we have in the final list to the terminal so let's print them to the terminal and run this script here with the Run script command and you can see the dates that are generated that starts on the 20th of June and the next date is the 30th of June and then we go to the 10th of July the 20th of July and so on so what we have here is 10 daytime objects that are spaced 10 days apart what we're going to do now is we're going to write a conditional expression here and we're going to get the total income of all sales that were made between those date times so if we quickly go to models.pi and go to the bottom here we have a sale model that contains an income field so we want to sum up the incomes of all sales that fall between each of these windows so for example the first and the second date we want to get all of the sales that fall between in those dates and then sum up the incomes and then we want to move on to the second and the third date times that we have in this list and sum those up as well so I hope that makes sense if we go back to the script we can start writing the code that's going to use these dates and it's going to generate multiple when objects from these dates so let's clear the print statement and let's get started writing this code so now that we have the dates what I'm going to do is for each one of these dates I'm going to create multiple win objects so let's create a variable here called wins and that's going to be a list comprehension in Python and we're going to create a win object for every single element in that dates list and these conditional expressions are going to be applied to sale objects which each have a date time field and we're going to use the range lookup here and we're going to pass in a particular value DT that we're going to get from the for Loop and we're going to also add a timezone.time Delta of 10 days to that particular date object so we set days equal to 10 here and don't worry if this does doesn't make sense hopefully it will after we finish writing the code we're checking if the date time is in that range between a particular starting date and the date 10 days ahead of that starting date and if that evaluates to true we're going to create a then keyword argument here and we're going to use the value object that we saw earlier and we're going to take that date and use its dot date function and set the return value to that date and we're going to do that for every date that we have in the dates list that was defined up here so just to summarize for every single date that we have in the populated list of dates that are 10 days apart we're going to create a win object for every single one of those dates that takes that date and also the date 10 days in the future and we're using the range lookup here and we need to add the equal sign here actually we're using the range lookup to check if the seal is going to fall between those two dates and if it does we will return that date as the annotated value so that's our list of when objects we can then add them to the case statement one at a time but we can also pack them in in one statement so I'm going to create a variable called case here and instantiate the case object that we have in Django and unpack all of the wins that we've populated above in that list here and we can also say an output field and I'm going to set the output field to a Django car field and we need to import that at the top just from the models module here let's import the car field and that's going to be the output type for the result of this case expression so I know this is quite complicated but hopefully it makes a bit of sense if not we can go through it here at the end what we're finally going to do and this is the last line of code in this video is we're going to use the sale.objects.annotate function and let's add an annotated field here of date range and we're going to set that equal to this case expression that we had above we're then going to get the values that we're getting back from that date range from that conditional expression and then finally we can call the dot annotate method and we can sum up the total sales here we're going to create an annotated film called total sales and use the sum aggregation function over that income field on the sale model so let's import the sum function at the top here and then we can go through this example one last time to see that it makes sense and let's print the result of this expression to the terminal and we can see the output that we're getting here so let's execute the script at the bottom and I'm going to bring this up so we can see the output we have a bunch of date ranges and we also have the total sales that were made between those dates so for the date range that starts on the 20th of July we have a total number of sales here a total income of 1 315 and for the date range that starts on the 30th of July we have a total of 912 so what we're doing here is for each 10 day window we're calculating the total income that was generated in those 10 days from sales that were made at that time period and we're displaying them in this case just on the terminal so let's walk through this code one last time to understand what's going on a little bit better it's quite a complex example what we're doing at the top is we're pulling out the first and the last sale dates from the database and then we're creating an empty list of dates here and then we're calling itter tools.count to get back this infinite iterator that we can use within the way we'll look below within the wheel Loop we are creating a variable called DT and this represents each date at each iteration of the while loop and this works by looking at the dates of the first sale and adding a Time Delta that's dependent on what iteration we're on in the loop and each time the loop iterates it calls the next function on this counter object from enter tools so that goes from 0 to 1 to 2 and so on and we're adding 10d increments to that first sale at each step so at each step we have a particular date object and while that is less than or equal to the date of the last sale that we pulled out from the database what we're doing is appending that date to our empty container that we created above and then we have have after that we'll look a list of populated dates what we can then do is and what we are doing is we're creating another list of Wayne objects in Django and these when objects are going to be applied to the sale field and the sale door objects or annotate function and they're looking at the sales date time field that captures when the sale was made and for each date in the list of dates what we're doing is we're checking if the sales date time Falls between that date and 10 days into the future and if that evaluates the true we're returning the value of that date as the annotated field so for every 10 day window we create a win object that's going to check for sales being within that window and then we create the case expression below we unpack all of these when objects within the case expression and we Define that the output field should be a car field and that's because the value coming back it's a date object but we want it to be represented as a string and that string is shown Below on the terminal you can see that here for example for the State Range it's shown as a string here and the final part of this whole puzzle here is the sale.objects.annotate function we're passing our case expression here to that annotate function and we're creating an annotated field called date range we're then getting the values from that date range and annotating over them again to create the total sales for that particular range and the total sales is simply generated by calling the sum aggregation function over the income field of the sale model so it's quite a lot to take in this is more complex than the other examples but it demonstrates how we can create a list of dates in this case and then create a list comprehension that populates a bunch of when Expressions that can then be used within the database to check if a particular role has a value that falls between multiple conditions and return the correct value based on those conditions and this can be good for performing aggregations in buckets in this case over a 10 day window so that's all for this video we've looked conditional expressions in Django we've looked at the case and when objects that can be used to build up these expressions and we've looked at multiple examples of how these are used in Django if you've enjoyed this video please like And subscribe to the channel and if you want to support the channel we have a coffee Link in the description otherwise thank you very much for watching and we'll see you in the next video
Info
Channel: BugBytes
Views: 3,175
Rating: undefined out of 5
Keywords:
Id: beVGYjMSRRk
Channel Id: undefined
Length: 36min 25sec (2185 seconds)
Published: Mon Sep 18 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.