The Beauty of ViewSets in Django Rest Framework

Video in TIB AV-Portal: The Beauty of ViewSets in Django Rest Framework

Formal Metadata

The Beauty of ViewSets in Django Rest Framework
Title of Series
Part Number
Number of Parts
CC Attribution - ShareAlike 3.0 Unported:
You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal and non-commercial purpose as long as the work is attributed to the author in the manner specified by the author or licensor and the work or content is shared also in adapted form only under the conditions of this license.
Release Date

Content Metadata

Subject Area
ViewSets will make your code shorter, more robust, and save you time during your development, if you let them. I have spent a lot of time dealing with writing view code, and dealing with all the urls, only to finally learn ViewSets. It immediately saved development time as well as making my code more simple. Generally to make a new, basic, endpoint in DRF for a model it would take about 15 minutes. That includes creating a serializer, urls, views, and testing it the browser. Now that same endpoint is more easily understood and done, all the steps, in less than 5 minutes. Leaving you more time to worry about what your new app is supposed to actually do.
Area Software framework Social class
Gender Software Expression Videoconferencing Video game Coma Berenices Cycle (graph theory) Fault-tolerant system Task (computing)
Point (geometry) Code Model theory Gender Electronic mailing list Set (mathematics) Database Electronic mailing list Software maintenance Single-precision floating-point format Uniform resource locator Latent heat Computer animation Operator (mathematics) Software framework Endliche Modelltheorie Object (grammar) Fiber (mathematics)
Filter <Stochastik> Digital filter Group action Serial port Code Line (geometry) Demo (music) Regular graph Declarative programming Wave packet Different (Kate Ryan album) Electronic visual display Software framework Endliche Modelltheorie Demo (music) Information Code Division (mathematics) Database Line (geometry) Group action Uniform resource locator Film editing Order (biology) Surreal number Quicksort Impulse response
Point (geometry) Web page Functional (mathematics) Information Demo (music) Software developer Real number Demo (music) Set (mathematics) Bit Client (computing) Cartesian coordinate system Flow separation Field (computer science) Connected space Similarity (geometry) Process (computing) Single-precision floating-point format Pattern language Website Software framework Pattern language Whiteboard
Point (geometry) Meta element Presentation of a group Serial port Model theory Demo (music) File format Field (computer science) Field (computer science) Symbol table Data model Uniform resource locator Computer animation Personal digital assistant Function (mathematics) String (computer science) Surreal number Representation (politics) Software framework Endliche Modelltheorie Object (grammar) Website Form (programming)
Type theory Parsing Digital filter Context awareness Computer animation Code Object (grammar) Model theory Computer cluster
Filter <Stochastik> Axiom of choice Point (geometry) Digital filter Serial port Code Cellular automaton Parameter (computer programming) Mereology Area Uniform resource locator Medical imaging Object (grammar) Endliche Modelltheorie Website Error message Parsing Dependent and independent variables Multiplication Theory of relativity Information Key (cryptography) Model theory Database Hidden Markov model Inclusion map Word Error message Computer animation Function (mathematics) Charge carrier Object (grammar) Sinc function
Filter <Stochastik> Point (geometry) Meta element Digital filter Matching (graph theory) Field (computer science) Symbol table Data model Computer animation Personal digital assistant Query language Resultant Social class
Filter <Stochastik> Point (geometry) Digital filter Functional (mathematics) Presentation of a group Manufacturing execution system Serial port Code Virtual machine Set (mathematics) Mereology Semantics (computer science) Field (computer science) Uniform resource locator Derivation (linguistics) Latent heat Different (Kate Ryan album) Term (mathematics) Object (grammar) Operator (mathematics) Query language Conservation law Website Social class Authentication Area Injektivität Weight Electronic mailing list Menu (computing) Database Bit Price index Line (geometry) Computer animation Query language Function (mathematics) Self-organization Social class Object (grammar)
Filter <Stochastik> Digital filter Functional (mathematics) Serial port Information Code Model theory Electronic mailing list Set (mathematics) Line (geometry) Hidden Markov model Mach's principle Mathematics Computer animation Object (grammar) Query language Traffic reporting Arithmetic progression Social class Form (programming) Physical system
Point (geometry) Web page Authentication Computer-generated imagery Execution unit Parameter (computer programming) Regular graph Uniform resource locator Software framework Website Extension (kinesiology) Social class Default (computer science) Domain name Authentication Pattern recognition File format Model theory Interior (topology) Inclusion map Computer animation Boom (sailing) Pattern language Right angle Figurate number Router (computing)
Web page Execution unit Digital filter Server (computing) Code Multiplication sign Authentication Demo (music) Group action Mathematics Uniform resource locator Symbol table Goodness of fit Computer animation Query language Energy level Website Router (computing) Default (computer science)
Lecture/Conference State of matter Code Bit Line (geometry) Twitter
Digital filter Serial port Code Euler angles Model theory Real number Maxima and minima Control flow Set (mathematics) Voltmeter Number Attribute grammar Software design pattern Software framework Software testing Summierbarkeit Physical system Social class Task (computing) World Wide Web Consortium Time zone Key (cryptography) Software developer Electronic mailing list Database Basis <Mathematik> Instance (computer science) Symbol table Inclusion map Category of being Process (computing) Computer animation Query language Normed vector space Order (biology) Pattern language Right angle Quicksort Object (grammar) Sinc function
Coma Berenices
the end of the of the of animal
and some
days in and around the room as if they were covering the area I checked fossil they based on our class that are during my lighting topic we only hear about 5 so I will see really don't personally it so 1st of whom I have but
when teaching and I grew up in Oklahoma and I a cycle the jingle com occur only have about 150 J. mostly task is to learn it and the education all genuine and continuously adding new video so I am possible check it out and striving watching all the accretion of that also seen shopper engineer and the accompanying tools called somebody is p and you were mostly gender work and will the whole stack of all and I also like 1 where the 2 latest things and learning about a sustainable farming in our culture and also cryptocurrency trading my wife is constantly said she's always curious every 6 months now with new thing in China were close by some what she never knows what it's going to be so with that so my wife and my
daughters where the 10 months old was was the grace and I did this picture on the way out of my daughter's life while taking pictures but that's expression of the so that was
given to it as this is about the sets and i in generous framework I feel like the 1 of the best place to start to really understand the sets because the stock is about the maintainability of code esthetics occurred and being able to read the code really fast quickly grok mentioned what's going on I'll start at the beginning where you have the
anatomy of an endpoint for a single model generally you have to do a single model you have to get the ACT vary please you with creating a list of all everything the details of a specific object database never post for creating and you have a point for update and to for leading and as a composer the fibers that we deal with and and during doing models and of the different prior operations and those achieved gender the generally made up of 2 use into your else by your creating list generally something like API such points at the base of something and then your detailed view your update and delete our say API coins in the idea or whatever the objectives and so the consider the basics everyone is aware of that knowledge I like to start from the beginning to know were going
so these are generally doing a few things so returning whatever is being used and how the up properly it's uh checking permission division commissions to the set it's getting a relevant data that you're after to yield displayed uh to the user it's generally serializing data I'm using some sort of surrealism in using as regular model serializer at the contact serial training filters that you want to search and filter data Zhang filters and your user generally and all that well and there are also duty action we want you to getting the display its leading into something really the new object in the database so is doing a lot of work on one-word with things is just and where to handle that so are you URLs are
basically doing what I want they the routing and the information were sending it to somewhere else and around the world I see that all so Canada 1 difference that there is with that you sets is declaring a single you also set to you have basically 1 you and set of 2 or more and that you're only you working with co declarations what cut explain what that is a little more home actually get to them I can't show you what's going on and a demo order take 59 lines of code including our imports of using function-based with a generous framework down to 22 lines again included import so it's kind of it's taking a lot of coders simplifying it creating a lot easier to work like easier understand the good thing
about this is you know it can be a little more quickly grok all the endpoints our worker somebody a speed we have a single page applications and work and bring it from an old codebase to new could be based on the framework and we have a lot of endpoints and we're going to call constantly as more and were only back in a single page applications were also backing off on client that we have a lot of that we wrote so and people out in the field doing work can sink down information onto the local here do stuff without having a a connection and then it can push that information back up at a later date know that getting the exact same endpoints so that we have for a single page applications to the been heavily used and multiple ways to accomplish and the exact same thing and so you don't have a lot of points and it needs to have the robustness to handle and at least those 2 scenarios and you need to understand them really quickly Nielsen common pattern to create on all points because when we add a new page to our sigh and to the other like client we can generally create and you're wrong 1 2 5 or 6 different points and we have a lot more stuff in our roadmap that site the so something that we really need to make sure that we get well we understand well we have formally us to follow on future for our future developers and future team members till the come on board see what's going on I can replicate exactly will done we understand company I call thank so that's
actually take a look at a little bit of a demo an hour ago during really go from the function based using real look at kind of how everything is gone I really think he's done the function this year well it's like if you will converge to a class-based you and finally will looks like as a while of the said at the end thank you'll see and then the last thing to show it is and how we have developed a process of testing the sets the referral is a good way to test about over us that separates the the
small the the president article when using presentation noted by Charles never use of forms of vessels in a world of skills so as I have beginning we have a model it's very basic they're dying a model name simple price in US dollars pricing based in Sobey calling for point I reveal that small fields and we have our string representation and so on with the show that they know that looks like thank very basic the the phonological work the the we the that the the the Mr. this way so the serializer sustainer jingo Moser generous framework model surrealism 0 point serializer and small surrealism Sitter model and it feels a little bring and available to display to users In this case we have our your URLs of appalling in to function-based for setting at your 1st URL to together with this and be able to create or not . object and we have our 1 for IDE to get the detail and there will be readily and update and we
have our views here the
by Sears are interchanged ignore those so the 1st thing I wanna do is so was a wooden context and we can 1 understand you know what is this doing because we have that we have this type of code everywhere already got well we good you're also say OK API
coins that pointless now our
the point was you well as log image required can give information example that information now in a lot of support since after another post to In the 1st thing to work they were doing it forgetting our data or applying all filters so I will fly and then we're serializing the data based on the last filter that we apply the key words setting the medieval literature and I I mean if for use of this you know we're getting a lot of information spread quickly that it works not that is to I have to look up a lot of things to really understand what on so when they were returning adjacent response with that data but in our doing a post which the post we're doing but parts of the data serializing checking valid and save but now I have announced on acts of our doing following required again Mrs. are exempt again they are details were passing in the request of the primary key argument in other databases than existed for foreign again better get serialize return response now a check for food passing serialised is about returning a response here with their or 400 status there's an error the finally there's the lead were deleting them out of the database and turning while that it was deleted very common function-based views little there's a lot of culture and if you you anything custom in there it's going to make this longer or more complicated and we have a lot of code like this at some point you in relate lower-grade college custom information when dealing with of my actual model data and the how I specifically said models so what are those 1 serial might use you're doing a many to many objects and you're passing and so in you Jason requests updated these to create objects well we have a lot of information in here you might be be tempted try create all those objects to do all that stuff the inside of your view but in reality the best actually view that is the serializer because you might use that same serializer in multiple that reviews and you want to handle that chunk of Jason insiders serializes somewhere else so this leads to an opportunity to do so the poor design choices and this is still valid still works and the end of the day is going to be using the fine as long as you understand the carriers to as concluding at are better
at Django filters resting a simple like filter for a point so this being in a basically case-insensitive exact so match for the name uh anyway just from the basics of men in this ordering of the of the case for some reason we want to filter out every single result was the name of it had been is just something arbitrary came up just show example so let's say that we want to look at our class used but
but the that
the so you are also slightly changed but were going acquire calling a list you and quite detailed you we're doing our as new I'm here because the cost based news or not calling the function correctly so the abuse actually summed up a
lot the cordless pointless you hurting from list create API view from generous now we see we have operation classes and we're checking for authentication possible but also the serialization now we know that this is in a serialize anything and then based on the serialized as the top will surrealist again as describing a minute ago if while you're dealing with many to many data related data and any handle letting the serializer you overriding thing in your views here you serializer will use of cortical magically in the way that the weight 1 the by the faults of generative work does not handle that and you have to him what somewhere yourself all that could be a completely different discussion further presentation because their semantics of all that well so there is also the filters that I was showing you an generally start with aging of filter back and and in areas that a bit older as an example we have our query set up in here so we know today this view is gonna start with this query set and everything that goes beyond that is going to be a derivative of this query set and finally we have the filter class which is point as kind of the main filter for all the individual fields and you know we can use other kinds of things but the primary filter of this of you so quite detailed use ordering retrieve they destroyed the idea so it now determined in 1 line that this you will get the data of the data into short data for a specific object database of all interest it's going to be up indicators these the point serializes through point term serializer as scanned again start the filtering based on the calling object so now the now which we compare cost machines of the function this used we've gotten the exact same information from from reading the us that we did In the conservation is there the class-based we used a lot faster and it makes more sense once we understand how fast these were I n I will hiring necessarily have the opportunity to inject code that doesn't in on the views in the 1st place because what kind of declaring these things are going to happen the past this using these you actually year and we don't really want you anything else this is what we want so and this is where the parts is the general because it can lead to drier codon and better organization of so while let's jump to argue
sets the last the
by so we're going to reports at the top and now we have our when you said and we only have 1 view as that's why a set combining multiple views into 1 going quickly tell reviewing create a list of doing other things you treverton destroy and everything is being based on a generic so rapidly determine exactly what this using you then we have the permission classes will point serializer I that we use gonna work on narrative filters preset enemy of class so now we've even no more quickly determine what it was going on pricing a progression long form function that used to be that's only a few lines quickly understand quickly we see what's going on and reality I was Sandy you like a market data you said I'm talking this chunk of code and a change all the information and now I have my complete you system in these none for market data no they had to be now is going on
and have world so that's when things get a little bit tricky as compared to a regular use Django is yet you or our side of a generous framework and being all rock formations of placed properly I generally can survive using just the people I have only ever written a custom rather for the exercise of writing a customer utterances that not even be what practice if anyone has these learning a lot about politicians and understand that it's better for why someone with that basically instantiating a rather you uh register AII your point of say coins in this place to review API such points and he said that you said that you will use Anderson now whatever you have read a coin sloshed is going to do everything else parameters in figure is there a of an extension on a euro per primary key yes OK now I need to go to day that 2nd class based view in a sense to the a bigger unit you give details to another week and they determines their based on age for such diverse model system and then it down here and the patterns we have just pretended it with API or domain included with Robert right world the is a standard Django your pattern recognition and the regular expression accessible as it tries to figure out what he's doing so well with that in mind had we've written basically variable coded this point on it to get something to work what's actually make make some make it do
something so repressor page authentication credentials you are required yellow but login now our what then
server repressor page reading all of our auditing are a big ways has the and the filter on it so far the but on the level ch repression I have provided ways there my there is going here and I can also does the filtering the and also gets my details and if it doesn't have anything returns a not found the kind of our world courses there were out of places so it return back and we've got a lot of that without a a lot of extra effort and then the last summer they want more readable code all so they with that and the day and about the time that I want to go off on a lot of you you know so also the your more good good to
go on and the and any upon Twitter rapidly when the line where the angle and in I don't like it and I'll put this code of 1 bit of a latent state populist party in experiment I have tax believer once a once gene can check amount to also just realize it and show you 1 thing I want to show you but they also the
whole in the
the tha
this so this is a test of our own system the set but 1 thing and then a but corpora pointed out by the corporate credit designers pattern and then these superstar developer in my in my opinion and enjoy learning and this is kind is pattern pattern discussed it came up with kind conclusion and the I love this and that begin makes a lot of sense so in order for this to be said to be fully tested we have this test system in place where were setting up a task so there were instantiating interview set and they were checking to make sure all of the instances they were expecting or set on that instantiated objects were also checking in the permission classes that we expect as well and are setting the serializer past In risk-aware checking the attitudes that are in use at and if you're you're if you might have noticed I don't have the query set it's because I figured out a way to properly chance that a new some sort of assertions work how this works in a like it's going up hopefully never changes it should and that's the big thing is this is so bare you know that your review sets should very rarely ever change and the reason you test like this because in this case while we're considering these attribution is he properties that attributes where it all as close themselves instead of just properties of class and we don't want this to change if these do you ever changed what we notified that they change were trusting that the you set will always work the way the research work so we don't see the need to go and pull immigration that's possessed every single scenario that use would generally and so that is also to human any so the question refinement of herself how how would you go about who sending a and the radial hold numbers in get pattern using discovery so that was the end of all data going out at those array of no actually how would you to begin with and then I'm having an issue with I want a big do you around and in abuse and how many said that an array of numbers in India gets will do a process this something I want yes of Nolan send but a bunch of a real number so on that get you will do well it's a select what do you call it a pony they do in abusive few have you basically have a occurred the URL that has some set of numbers and you need a process of work but that would generally the and break outside of of of so you have this of people of the the of the of this is kind tightly defined you am and Jason data of all an that I wanna save the were getting out the gist of the cave of the object in the database I We because of processes like that and you get the job done in the and override stuff so the user but I generally probably over break off this you last this pain and then actually removed the specific makes sense well if everything else is going be the same great you your our of basis left the and there are so that way doesn't activate that were the I did when you're not implementing every method on a particular view set do you assert that the method isn't present in anyway so town of query extraneous there's a but so that I don't use again and I don't have say the always a get together lives and I wouldn't have this list following here but that would mean that we get for with no primary key I would not activate that H 2 verb they would return in World War yep so yeah that's kind of the work on automatic switching zone that's kind of some of the base of class-based use of the Django as long as you know how that method on the other hand you have been assigned to the available years after the right since treason that's feature of the framework itself and as you can because they have that they should have passed without at the beginning of so that's something I don't need to worry about that is it is something we look at it as we don't want to test the framework on test our code but with sounds like that is being a vaccine names how high how high so high that the the