DjangoCon 2015

La interfaz Meta de Django 1.8

Russell Keith-Magee  · 

Transcripción

Extracto de la transcripción automática del vídeo realizada por YouTube.

well good afternoon Cleveland as I said my name is Russell Keith McGee my day job is as CTO and co-founder of trades club we're an Australian software as a service for trades people plumbers electricians carpenters people like that but the reason I'm

here today is Django I've been a core team member for the last 10 years president of the DSF since 2011 I've served on the Technical Review Board for the 1.7 1.8 releases and I'm also on the security team but one of the other roles that I've

assumed over the years is as a mentor in the google Summer of Code for those who aren't familiar with the program Google Summer of Code is effectively an internship for college students open source projects apply to have students the students then apply

to be a mentor in that particular project Google very kindly picks up the tab of paying them a stipend in exchange for 12 weeks of contribution to an open source project the only catch it needs to be open source and it needs to be done as solo work mentored

by an expert from that project now this year this this year is actually one of the first years in about6 that I haven't Adak been actively mentoring a student but over the years the google Summer of Code has resulted in a number of very large features

that have been added to Django the fact we've got multiple database support the ORM aggregation API is the systems check framework in 1.7 and in 1.8 a formalized metamodel ok so what is a meta model and what is meta programming well in short meta programming

is when you're writing code that can reason about the code that is running it's especially useful when you're writing generic frameworks because instead of encoding specific situations you're encoding how to respond to a generic situation an

easy warm up example meta programming can be found inside Python itself let's say we're going to do some graphics ok so we're going to need a point class to represent the points on our graphics graphics cans of canvas we're going to be both

2d and 3d representation so we're going to use some sub classing ok nothing too challenging here we've got some initialization methods we take some arguments we instantiate them some we want to be able to output those points in some way so you're

right so now I put methods your right output point for a two-dimensional point an output point for a three-dimensional point okay nothing too challenging there and when you run them you get exactly what you'd expect but what you really want is rather than

having a separate method for each type of point you really want to have a single method that will take an object a point of any type and output it appropriately and that's easy enough to write as well you know if you're coming from a language like

C++ that doesn't have meta programming tools or Java which has meta programming tools that doesn't encourage you to use them that much you might be inclined to do something like this you do a class check okay we've got one method called output

point we're going to print the things that we know are common the x and the y coordinate and then if this point that we've been given is an instance of a 3d point then print the Zed coordinate as well okay and that does work there's nothing wrong

with that but a more pythonic approach is to use metaprogramming what you do is you can every you can check for a class if you check for a class that you're only checking for 3d points that will work but if you check for properties of the instance you

can adapt to any type as long as it has the right attributes and so python provides a tool provides a built-in method called Hazara that is what you're checking is rather than saying is this object of this class you're saying if this object has the

attribute Z then print it the worth is even more attributes we're going to start modeling Doctor Who now so we need to include time into our XYZ coordinate so that means we've got to do another has a tragic right well no because again we can use metaprogramming

here every Python object has a special attribute named dunder dict that contains all the attributes of that instance so second line there it says Peter object that's all the attributes of that instance we sort those and we iterate over them for each of

them we're going to print out that attribute the name of the attribute and get the value of that attribute off the object P now the thing that's interesting is that that definition will work with any object not just points you can point any Python

object at that method and it work as long as the the attribute can be out what is a string that will successfully output a representation of that object now that is admittedly a very much contrived example you probably wouldn't actually build it that way

you'd actually use the the wrapper method or string method on the actual class itself but it's enough to demonstrate the point by leveraging metaprogramming you can do some very powerful things with very little code and in pison at least it's fairly

easy to understand code ok so what does this metaprogramming mean when we move to January well it means being able to do some the same sort of thing we just did with Python objects but do it with data base models one obvious place this is really useful is

model forms I have a Django model it has a bunch of fields I want to be able to display a form to edit an instance of that model now I could define a form instance for each of my models and then add the fields that I want to edit for those on those models

or I can use better programming using metaprogramming I can iterate over all the fields on the model and for each of those fields added up input that's appropriate for that form where's that particularly helpful in Django admin Django admin allows

[ ... ]

Nota: se han omitido las otras 2.891 palabras de la transcripción completa para cumplir con las normas de «uso razonable» de YouTube.