DjangoCon 2015

Uso de PostgreSQL en aplicaciones Django 1.8

Christophe Pettus  · 


PDF (pincha para descargar)



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

okay I going to punch right on in because we have so much stuff to talk about specifically let's we're going to plunge right in on Jango 17 introduce native migrations and jang geun 18 introduced Django country postgres and there's some in it there's

so much cool stuff in here and so little time so let's plan forward the first two are the one who says most of what I'm going to talk about in Django contributors was done by marked Hamlin in believe he's a Kickstarter IndieGoGo i figured i think

its kickstarter project for it who deserves endless praise for these wonderful new features so first i'm going to talk about migrations and everyone's who said wait my visions are postgres specific why are you talking about migrations and the reason

is that a lot some of the stuff we're going to talk about their an enabler for other things and jungermann seven migrations are just amazing so if you haven't used them just you know go to your laptop I won't be offended install move to using migrations

and thanks Andrew for those so a quick review of for anyone who hasn't used the 17 migrations or have been treating it like a black box migrations are built around this notion of operations each operation in a migration use the database backward forward

through its schema migration timeline so it adds a field adds a table that corresponds to a model but adds does something like that there are lots of individual operations but there are two super interesting ones for our point of view which are running one

sequel and create extension these are operations that are built into come with the Django framework or framework now and you can use them in your own migrations run sequel probably you can guess what it does it applies Ross equal directly to the database now

everyone's saying oh god is one of those database guys talking about Ross eco I'm going to launch again but really work with me here it's it's very very handy um specifically it's very useful for things you can't do directly in using

the models yet like creating indexes for some new types we're going to talk about there's also a create extension operation which runs a create extension command you can sucker serve pattern is forming in this naming convention or specifically the

extension mechanism in Postgres which was introduced in 91 something like that is it's a little bit like pip for post words is it allows you to add extent packaged extensions into your database previously you had to drop a dot s 0 file here and run the

sequel here and do all this stuff and it was just horrible specifically we're going to talk about this extension H store which is a which needs to be added to your database before you use it doesn't come with the core postgres installation a lot of

this will make more sense later at the moment I'm just teasing you um one thing to notice about H stores if you have it adds a query to every can't i'm you connect to the database but we'll talk about that also so that's our that's

all you need to know about the new extension facility let's talk about 18 18 adds some new field types which are array fields range fields an 8 NH store field 19 is going to add some more cool stuff but this is what we get in 18 so in a ray field in Postgres

since pretty much the dawn of time arrays our first class types you can have a single column that is an array um now database you know old school database people like their heads explode at this point because this is denormalization and this is horrible forget

those people they're old and in the way um these this is a really really cool feature an array field is now a field that you can import from Django contribs crest and it lets you use those directly before there were tons of field of things you could find

on django snippets and things like that that would let you do it but now it's fresh out of the box and these map into python arrays why i said array here is because i molded in the way what I mean course is a list one thing to remember is the postgres

arrays are of homogeneous type unlike Python lists which are not so you get you pick a type int string something and that's what you get in a postgres array Python will let you put of course anything you want in a list but don't do that if this is

what you're trying to model so the other thing is the Python multi-dimensional arrays are rectangular remember Fortran just like that is it the great that we're honoring our ancestors in this way um so the traditional multi-dimensional arrays there

you say I'm going to create a three-dimensional way with dimensions of 5 7 and 10 and the whole thing is appears it is populated for you they can be not individual entries can be null if you need to represent a hole in the array but it's not like it's

not like a list of lists structures where each individual element could be any size you want so once you've got it as the dog chasing the car said now that you cut it what are you going to do with it you have a refill queries the basic one is contains

the contains matches if the array field on the left contains all the all of the entries of the list on the right so ABC contains a B but it does not contain ad order is not important in a contains query so you can continue they can appear at a thing you're

in any order you want you can also have contained by which is effectively the reverse matches if the list on the right contains all the entries of the field on the left so this this is not contained by that and that is not contained by that but that is contained

by that we order is not important here either ok that's easy and then you have overlaps so which is the any version of this so this overlaps this but that does not overlap that and those and those return a boolean then you also have the predicate underscore

underscore lens which returns length which returns the length of the field on the left as an integer so this is kind of approximate syntax you wouldn't actually write this in your Python but you get the idea that if it's storing this value you apply

length to it you get to now something to remember about this is the unless you've created a particular expression index which we'll talk about in a bit this is going to do a full table scan it will pick up every single row in the database that for

that table rattle it to calculate the length and filter it so you probably don't want to do that on a big table you can also apply transformations to it such as an index this takes a light for example this filters on everything whose first element underscore

underscore zero is is the string a if there's no array 0 entry 0 for example the the the the raid is of length is of length 0 it dumb it doesn't match it simply returns false there's no error so if you put five million there and there's no

5 million link to raise you'll just get back false for everything one thing is directly you can't specify this programmatically you built in as if you build in a zero there unless you're going to you can do string substitution and use quarks to

do that of course you can also slice the array which is pretty cool you can say okay anything whose entries 021 are that or probably more usefully 022 contains a you can also index array fields um so you can just say DB index equals true and you're done

right wrong sorry this it creates a b-tree index on the array which is pretty useless for an array um so this is one of the downsides right now of using these is you have to if you want indexing on them and you almost certainly do want indexing you have to

do some special magic so let's talk about that but first let's talk about how post Chris does indexing this Chris supports different kinds of index which most people who are just using the Django arm never see because you only get one kind out of posts

out of it which is unless you're using geo Django which is a b-tree index betray indexes are great they're really they are new the the optimal solution to a particular problem which is they're very fast they're compact and they provide total

[ ... ]

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