Mighty Model Managers

Video in TIB AV-Portal: Mighty Model Managers

Formal Metadata

Mighty Model Managers
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
Model Managers are an amazing part of the Django Framework. When put to use, they can bring enhanced readability, encapsulation of logic, increased security, and performance. But they're often overlooked - even by those with years of experience. Let's fix that. We'll go through examples that demonstrate how easy Model Managers are to integrate into a project and why they're so important.
Data management Computer animation Bit rate Model theory Mereology
Metre Complex (psychology) Process (computing) Computer animation Software Personal digital assistant Natural number Right angle Cartesian coordinate system
Complex (psychology) Computer animation Video game Family
Trail Functional (mathematics) Code Multiplication sign Real number System administrator Mereology Semantics (computer science) Encapsulation (object-oriented programming) Number Mathematics Goodness of fit Core dump Cuboid Energy level Software framework Stability theory Default (computer science) Model theory Moment (mathematics) Projective plane Electronic mailing list Sampling (statistics) Mathematical analysis Bit Instance (computer science) Line (geometry) Cartesian coordinate system Symbol table Data management Word Process (computing) Computer animation Logic Query language Internet service provider Self-organization Right angle Object (grammar) Resultant Reading (process)
Email Link (knot theory) Code Model theory Electronic mailing list Instance (computer science) Cartesian coordinate system Semantics (computer science) Encapsulation (object-oriented programming) Computer animation Logic Query language Right angle Text editor Traffic reporting
Point (geometry) Code View (database) Multiplication sign Set (mathematics) Mereology Field (computer science) Estimator Different (Kate Ryan album) Operator (mathematics) Database Authorization Office suite Information security Weight Model theory Instance (computer science) Cartesian coordinate system System call Hand fan Data management Arithmetic mean Process (computing) Computer animation Query language Text editor Resultant Fundamental theorem of algebra
Context awareness Code Multiplication sign Set (mathematics) Parameter (computer programming) Mereology Disk read-and-write head Encapsulation (object-oriented programming) Optical disc drive Response time (technology) User profile Computer configuration Different (Kate Ryan album) Hypermedia Software framework Physical system Social class Graphics tablet Arm Fourier series Infinity Sound effect Bit Nominal number Special unitary group Instance (computer science) Category of being Data management Chain Order (biology) Right angle Quicksort Row (database) Filter <Stochastik> Point (geometry) Web page Game controller Existence Real number Field (computer science) Attribute grammar Number Goodness of fit Database Software testing Default (computer science) Information Model theory Cartesian coordinate system Cache (computing) Word Computer animation Query language Personal digital assistant Charge carrier Object (grammar)
Software testing
poet of it has to it so if a if we were to put this it in the sense that KKT quite so big the rate common out early
appreciated found this talk is called Mighty model managers is said so 1st of all I have to give you all probs free-falling years worth 40 in is beautiful outside here talk with me about part of jingle course has been around for years so you were the true believers of the resulting of fat so for really talking about model managers specifically very pretty wild manager subclasses and how that means relies much easier and it really does so get excited so 1st of wire we here and not like an
existential sense them you might wire we here as meters of thought about it where are job
we and for me I think that really made the software is primarily about the process of managing complexity so depending on the case no sometimes arranging complexity on behalf of users or the complexity in a business process but it I think is that back in nature were doing it right and were managing complexity for ourselves in the applications of may and we could probably all agree on at least some so why wasn't he thought about managing complexity what a wide right
what is my family so my life I have 4 amazing kids of their ages 6 and under a love it all of them very much but I learned a lot about you know idea
also I I work for a company called the fellow work in health care which is also like that of complexity but that's not really is viewed as a picture of 1 so we also why don't talk about
monitors that the thing is that they're part of the core framework the documentation is spot on there really useful and I feel like overlooked in community I have a bunch of friends and you know we talk and I feel like it solves a number of problems of people really are looking at them I think it's 1 of those things get it working and the way so 1 my goal is that we can talk about this today and you'll be inclined to take at least 1 thing away from that moment 1 of the applications were any get from so we aren't there are a ton of benefits to the using manages the improves semantics of readability encapsulation of logic is a means an there's a lot more but this is a short talk so the we on social thing and how many here in attendance use model managers in an OK so would be on so for any 1 of you raise your hand is a little bit of a trick question sorry expense but if you use the r around it all you use model managers in 1 way or another so this symbol winery near animal that objects that all of this called the objects objects is the default model manager the Django gives you write out a of lot of answer right here is providing us with a query set back you were thinking also getting all instances of line and 1 not in principle you wanted still more explicitly you can actually go to write out this is what's happening free behind so when you define animal you could put objects equals models that manager he would accomplish anything you would be really we the so what's the job of a manager is that the only job is to give inquiries are not real so the 1st job of my manager is to control agrees that you get back when you want inspectors but aside from that a 2nd job is revived stable level functionality for the models so we think of it this way if you wanted to implement new functionality on an instance of a model you create a method in you put on what you want to provide stable level functionality of some and operating in all the instances of model you would create on Analysis manager instead written so as is usually the case that if you start tweaking things a little bit you can get some really interesting results so the demonstrate that let's talk a little bit of our sample project so we get together with our team we decide we an application for keeping track of the people within organisations this really simple it only needs to remember 2 things about person the name and the role of so you say no big deal on that your reader an author on so we say no big deal you had that code up this model right here in reading speeds go across into the admin now we can we can start that in the greater model real simple when we talk about his person person that objects that all and then you can use the semantics of this are a little weird and we all users see you know model but objects that all the time it's commonplace to us but he want object people are people so if 1 make that slightly more semantic is a really simple all you have to do is as we said before you can still use the default managers on the outer box think go this rename so from now on if I wanna Corey more semantic way my list of person objects as the person that people but all against the result is a little bit more semantic all this is a small change honestly readability is important I think is community gets that in general we have a lot of emphasis on good quality with have a a number of other things you don't take my word for it as
trade traded all talk about this yesterday and should be happening some so another thing
you like is this is a really matter and it does and I think that I can convince you we're I wouldn't need the model looking like this listing last persons is not a great way of representing the my instances of person you would go your model you add the proposed name for all of the right to do it that way the thing is just because the end user is seeing you change the way we query our models doesn't mean that that's not what we
so we talk a lot a lot a lot about semantics and some of the stuff and not everybody be convinced that it works but there isn't much more concrete benefits that had the encapsulation of business logic is really important especially as the application for the girl so say someone comes to you and me as I need to get a list of all of our company that to say we're glad person not people that built the always schools there's all of our editors no problem in the issue is that that query most likely is going to be replicated throughout your new code all over the application the need to create a report of all editors right that Peterson e-mail message all the Republican right that really going to be sprinkled throughout and yeah it's it works but you can wonder link is is the right way to do
it Vernon does nothing is the so we do
a lot better than so mobile do is will set up a manager subclass this it specifically for editors there's there's 2 quick that 1st will subclass manager and only the views providing so in here you can see where do work when the filtering inside the manager this is all rest fans research to represent managers in our model so we can be the existing model and that the existing manager and that will set up a reference called editors who turns are due to what you meant the not a query process is a little bit different but it's really clean again readability so person that editors that osprey preselect so it's very obvious to anyone who's a code new members and mean everyone understand exactly you in so what we want to say I and you replicate the process for all the editors not the only person I care about really straightforward so I'll create another mandrel called often operate in represent my model just like this and they can live right alongside the other really know you mean you can have in the process of acquiring person authors that all so this is this is familiar everyone's going to get this using that the there's some drawbacks of this approach so because we are getting a query set back when we say person dies editors that all you're getting Aroclor jingle for each basically there's no customer operation you don't have any way to inject with your these were creating a subclass every time we want to have 1 diesel in short there's gonna start to increase at while specially needed I'll do an intermediary step of so it'll work for now but there's probably with by is so all we can do is we can instead a subclass queries so now I have 2 parts of this process I have a person queries that and understood exposed authors and editors and that's remedy might go decrease as filtering itself on and I created new manager all person manager and understanding set up estimate for have authors and editors and all it's doing is appalling it's going to take their query that that authors so I can throw away my other editors offices and replaced so far Our model now don't like that from were and have another a person from only 1 minute call people and points directly for so now this is my career little is slightly different that it's getting fat so I have the ability as a person of people that editors and my simple person that people that authors the results for instance now I've a subclass queries that my cousin operations are now here so I can she's together can do that so I could say person people authors editors which that doesn't make sense because you can only be 1 or the other but I will show how that comes into play the you know the gene gets back together and they say that is great but we need to be able to track whether a person is 4 times part no worries there is gonna be the model so we add the point that that review because of the no now will head back to our queries that will add some new methods for full-time written part-timers there this mean the filter in hiding away and then I will next go over a few manager and add those supporting that there as well so now this is on when I query against this model 1st that people of full final and fundamental fine employees security that but also I can change by saying full-timers that offers to chemical and wanted or part-timers of the united part-time offers light as a leader is I started learning about this but there's a lot of people if they were just this essentially our a manager is acting as a pastor there's gotta be a better the current so as of Django 1 7 some of you may be familiar there's act there is a better way to harm will we can do is we can SGD go to extract the manager straight from agrees that that we provide and it'll do based on whatever methods of inquiry set has on so what we do to make that work is I will say people equals person query said that as manager now we can go even manager and renowned anything else is doing now were only and have worries that with the methods of the of the weights of sports hunger model of stronger the of the current we so in healthcare healthcare is not you really really tricky there's a lot of things that kind of doing all the work or application you know we had these like softer elites or on the different status fields the things you want filter where fillings comes up all the time so
managers are terrific option at preventing mistakes that happens it's like a like a joke about this a way of preventing for protecting future you from present this happened I'm at my own code that I wrote that for the schools so the bond that seeing aspect together in the say the leading uh leading the person instances from a database system in which you have you know the softly you say I I got the you got an is active field model adjustable in real but it's so easy to forget the filter and every time you run a query you have to remember that is active the chain of filters or you're an end up with that it would be so then anybody here have some like that before you all that might have grown I used to be breaking a nominal like to really bad place this so higher anyway so but then depending on the industry to it can be harmless in sort in a major and minor and situation so all we can do this is we can leverage always encapsulation that we have built into our application and really use it for so their private method here called active amount assumed that someone is bring my model doesn't need to use this active on the 2nd that explicitly there's gonna gonna get back the records by using the so all I can do are lead to do this internally within my full-time response times better understand a chain in the called the active right in there the now by default when I when I in creates this model I'm only going to get back at Ghent no had really different I did have to go rewrite all of my my queries against the person model and the add is active of the treated and there I could just means which is not nominal you can do this with things like stock-based Ferdinand play records or there's a million of an application for a really anywhere you need to hide you can you can sort of capsulate that school career the without causing so on the were and include only active at all and there's only 1 carrier this approach so you may notice were not overriding the default worries that that word burning the non so if we were honored out all the person that people but all we would get inactive records and that's by design you know you don't have to do it this way but there's 1 thing you have to really think about keep in mind is that we you assume control of the floor manager for model what you're doing is you're saying I wanna control the instance of a controlled increase that the get to return my model under all instances most of the time is referencing out from within your application that exactly the but it also counts for things like if you're running a on and don't is near you scenario you may want so he every use case is going to be slightly the amount mileage may vary is depend how you want the other things that note is that there are are specific methods that you can call for the that's CiviContribute you can set on a method acquiring so that when you call as manager they don't necessarily come over by the points of the new so for instance no private methods from over by default as in the and there's also this another attribute called queries that only that that reality but that only will prevent those methods from coming and so the the height something you can this coverage on so testing is outside the school of thought like it's pretty easy to see how this is a big win for the her as coverage in general from with the application so I really were both on to you if this query 1 inside and that you don't need test for a at 40 different of the within your application so the big winner of 1 thing also 1 note is another part is coming up directly after this is actually in this room on lighter note agrees that that and it's actually a much deeper dive paired vectorscope a little bit this part of the abundant for but on he's going to really dive deep into using I have objects in U of X and you can write crossing hopefully you really complex and research the Paris really well with this you know what in there which are going to do all those complex things and have the so custom of a metaphysical uh you save everyone else the going up here also when you a quick shot out on this part was really heavily inspired by the documentation for the model managers that it's out there and anyone who is interested docs they're doing amazing work out that when I was 1st the department 5 years ago the real reason that I was wrong words the Django framework is the quality the PAD support and documentation I think honestly it's unparalleled and so think it'll sporting on real quick as time few
that thank you very much alone the bit the in the battle that got me thinking could you could you put could you override the not all method on year courage said manager it creosoting might neighbors mixed up yeah glass of n to put a keyword argument in at that defaults to say truth so I'm thinking of younger is active equals true in the news that the filter underneath would there be any catches or a scary side effects of overriding . all it's good question on I hit say it with the fourier but I do know that what you wanna do is already in of the words that in order to forget what they're looking for in Uganda has it's by the time the 1 and I don't see why that wouldn't work place I was doing some experiments with overriding stop at that here and I wasn't sure that knowing a crazy but I do believe that the uh you could pass on 1 the only thing is is that it's a little town little at odds with it we just have to be a really known quantity for anyone agree to model local with this is that make it really straightforward that those just thinking of his case in which 95 % time you wanted active things you could count on small underspecified 1 and inactive records and among the yeah you fairly good at the other thing that I've seen to is that you can actually make so I I made active head up by them and they see it as a public method and then you also for the public method that really includes everything but you may some obnoxiously long names so someone's doing it totally opting and that I want I want everything including bad records the leave records and everything so there's a lot of different ways to approach I think it's such a generic concept because of thank you the yeah have you have our I tried using model manages to do other things besides filtering on thinking just as examples like logging our initiating but so are putting things in caches are anything like that désolé there was 1 thing of you about it over which is the really great way of catching because your because we're 1st means composable idea of the together all these filters you really it's UMLS at the Russian all catching idea where you can actually small sets of queries bring them all together so absolutely it's really good way of this we experimented with this with an outer thing about using this as a way of logging access to certain models is a little bit hard to wedge the user context into query of but absolutely you can do a lot of things they're not necessarily very related in then the existence of the we the the I think it so could be from right here but overriding the objects property on our model on to select persons icles snowfall manager and that's a leaves objects on as accessible problem of model for it is not so when you are what happens is you will look for the 1st instance of a manager that encounter on all you could you could still leave got like so you're worried about the compatibility and you get 2 references that is the manager 1 called object 1 of people make them with that of but that object would not be available of using up to explicitly call that out yet because yeah Django won't automatically give you if it finds the the thank you for my question the you have something that and if that is at play with that use people and set of objects that play well with generous reward with I you I can I can use that I know I would guess yes because that with the he all-around does is it does it this isn't an awareness does discover what instances of a managers class or subclass of Brazilian model probably leaning on a warrant that but as soon as that the question and I just really like tell your your names worked out most of the methods and stuff and you in a wave as curious say did you have other clever arm ways of naming things with uh when you're like doing things where you're adding additional select related so but that to something like so it keeps the ATR real common example would be listing comments on a page anyone and do some user profile information with each comedy do select related the common thing you want with a lot of queries over that's here saying that for a greater same data different ways 2 different manager but 1 would include additional data 1 with not to cracked yeah so yeah you can certainly do that that media so wished as I mentioned you can have really infinite number of managers living alongside and others so on you know if you're clever body to probably even only leverage a lot of 1 based query said subclass you about your suppliers conclude additional information and also in that OK and as I was more interested and had if you firstly come across the always I really get naming things it seems had energized very readable by yeah I'm not personally OK for a a rat applies the sun and and thanks to thanks to the costs
thank tests