Django Content Types framework / ContentType model

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to take a look at jango's content types framework now this is an application that's built into D jangle and it helps you track all of the models that are installed in your application and you can use these content types to help build generic foreign Keys between one model and possibly many other models we're going to see an example of that in the next video but before we do that we need to introduce the content types framework so that we can understand how jangle creates these generic foreign Keys let's get started so I have opened the page on the content types framework in jangle this is the documentation which I'll leave a link to below the video and we're going to just overview this at the moment and then we're going to write a little bit of code and we're going to see what kind of effect that has on the database now at the heart of the content types application is a model and that's the content type model and instances of the content type model they represent and store information about the models installed in your project and new instances of this content type model are automatically created whenever a new model is installed in the application now let's start demonstrating this now I'm going to open VSS code and I have the code we've been working with in the orm series open but this is a standalone video we don't need to reference any of the code what I'm going to do is start by looking at a typical settings.py file for ajango application so settings.py when you actually create a project with jangle you get a list of installed apps and these installed apps are what work together in order to create the functionality required for your project now of course you can create your own apps and you can use third party extensions but Jango has a number of built-in apps that are included by default when you create a project and one of them is the content types framework so as it said in the documentation this tracks all of the models that are installed in your application now on the left hand side we have the core application and I'm going to go to the models.py file these are the models we have been working with in this series including the restaurant model and we also have a staff model staff restaurant rating and a sale model and in the last video we also added a product model and an order model so we've got quite a lot of applications or rather quite a lot of models in this application what I'm going to do now is open up the database so I have the database browser for SQL lights open for that database and you can see all of the tables that we have in this application for example the restaurant table table we can browse through to the data that's stored in that table and we get back this data here now what I'm going to do is show one of the tables that we've not seen so far and that's the jangle content type table so let's right click that and we can browse that table and what we get back is a database table that contains three columns we have an ID column then we have an app label and we have a model column as well now the app label refers to the name of the application in this jangle project so we have an app called core but we also have some of the built-in apps in jangle like the admin app and the O app which has models such as user group and permission we also have the content types application that we're looking at in this video and that as it said in the documentation has a model called content type that we're going to look at very shortly in this video and finally we also have a sessions application and that's for all of the session related work that you can do in jangle now what we're going to do is we're going to open up vs code and I'm going to go to this models.py file and I'm going to define a dummy model at the bottom let's just call it dmy model and that's going to inherit from the jangle model class and we're going to add a single field to this model I'm going to call that field name and that's going to be a car field with a length of 1 to8 so let's now on the terminal run the command python manage.py make migrations and when we execute that command we get back the migration file here we can then run python manage.py migrate and what the migrate command does is it's going to create the database table in the database for this new model that we have but it's also going to do something else if we go back to the database browser for SQL light as well as the new table it's going to add an entry into the content type table that we have here so if I refresh this table look at the bottom of the set of rows that we have we get the new model that we've just installed added to the content type table and it has is the ID of 14 so whenever you perform any migration in jangle and you add a new model a new database table what happens is a new content type record is created in this table and that tracks which application the model lives in and also the name of the model so what can we do with content types let's go back to VSS code and I'm going to open the or script.py file and we're going to write some code that uses that content type model class that is provided by the content types framework so at the top here I'm just going to remove these Imports and I'm going to import the content type model you can get that from Django do contrib do contenttypes do models and we then have a run method we can fetch all of the content types by running content type doobs do all so all of your normal D Jango orm functions are available to use on our content type model and then once we get back the content types we can print them to the terminal and once we've added that on the terminal we can run the script but what I'm going to do first of all is I'm going to move the terminal panel to the right hand side you can do that by right clicking terminal and setting the panel position to the right hand side and let's minimize that a little bit and we're also going to remove this left hand side here this file browser so let's Press contrl B or I think it's command B on Mac so what we're going to do in order to run the script is run the python manage.py run script command which is provided by jangle extensions and the name of the script is omm script we can run that and we get back all of the content types in this application and these content types each model has the field that you see in this databased table here so we have a field called Model another one calleda label so you could access those fields for example in a list comprehension C do model for CN content types if we were to rerun that script you're going to get back the names of the model classes and of course as well as all we can also use the filter orm statement and we can look for only for example the models that are in the core application so we look up the app label which remember is a column in this table and then we only get the ones that have the app of core so let's try and rerun this script and we're only going to get back the models that we have in the core application now the content type model its manager has some methods that are not available on a normal jangle model we're going to see an example of some of those now and just build up our knowledge of these content types before we look at generic for keys in the next video so let's go back to the documentation for jangle on the right hand sidebar of this content types page we have this section here and that's for methods on content type instances so let's start with this method here a content type has a model class method and that Returns the actual class of the model that's represented by the content type instance let's see an example of this with our restaurant model so we're going to go back to our script here and what I'm going to do is I'm going to create a variable called content type and that's going to be equal to content type doobs doget so we're now using the dog function which should should return a single row a single instance of a model and in this case it's going to be a single instance of a content type model we're going to set the app label to core and we're going to set the model to restaurant here and I'm going to format that into a new line so we can see it better so all we're doing here is we're fetching the content type where the application label is core and the model is the restaurant model and that's going to give us back this specific rule in the database table for the content type that represents the restaurant model so what can we do when we have that content type we can get the actual model from that content type so let's do that just now we're going to create a variable called restaurant model and we're going to look at that content type and we're going to use the model class method and we're going to then print out the returned restaurant model to the terminal so let's go to the right hand side and we're going to run this script and we can see that that gives us back that class that represents the restaurant model so what can we do with that we can actually use it as we would with the normal reference to the restaurant model so for example we can call restaurant model doobs doall and that's actually going to return all of the restaurants that we have in the restaurant table so if we have the content type that represents a particular model we can get that model class and then call normal methods on that model now it might be a bit confusing at the moment to do things this way why not just query the restaurant model itself it turns out that this kind of technique is going to become a lot more useful you're going to see the use cases of that more when we look at generic foreign Keys now another useful method on a content type object is this one here and that's the get object for this type method and that takes a set of lookup Arguments for the model that the content type represents and it does a DOT get lookup on that model so let's go back to vs code and we're going to see how to use this particular particular method so we have a content type that represents a restaurant model what we can do is if we remove the model class we can use the get object for this type method and then we can pass lookups as we normally would to the D jangle doget method for example if we wanted to get a restaurant whose name was Taco Bell we can execute this statement using the content type do get object for this type and that's going to return an actual instance of the restaurant model so we can then print that restaurant model to the terminal and we can access attributes on the restaurant model for example a restaurant in our database system has a Latitude so let's execute this and we're going to see what we get back on the right hand side we get back the instance of the restaurant for that restaurant with the name tackle Bell and that's again using a DOT get lookup under the hood but we do it from the content type model now this might seem a little bit boring but I think this is fundamental knowledge in jangle for when you actually use generic foreign keys I'm going to go back to the documentation and if you scroll down a little bit here we are going to see this section here I want to read this just to highlight why you might want to know about these two methods so let's read this now together get object for this type and model class enable two extremely important use cases the first one states that using these methods you can write high level generic code that performs queries on any installed model so instead of importing and using a single specific model class you can pass an app label and a model into a content type lookup at runtime and then work with the model class and retrieve objects from it and the Second Use case states that you can relate another model to content type as a way of tying instances of it to particular model classes and you can use those methods that we saw above here in order to get access to the actual model classes and this system of using content types in order to tie together different models it's actually used by jangle for jango's permission system and we're going to see an example of it in the next video when we add a generic foreign key to particular models let's finish this video by looking at the content type manager and then in the next video we'll get on to the interesting stuff with the generic foreign keys so content types that particular model has a custom manager it's called the content type manager and it adds the following methods we have a clear cache method we have a get for ID method which looks up a content type by its ID we also have the get for mod model method and this is probably the most useful of these and what that does is it takes either a model class or an instance of a model and it Returns the content type instance that represents that model and there's a couple of other ones here we don't need to know too much about those what I'm going to do just to finish this video is go back to the orm script and we're going to remove this code here and I'm going to add some new code so we're going to need to import from core. Models uh the rating model in this case so let's say that we want to find the content type for the rating model what we can do is we can create a variable called rating content type and we're going to set that equal to the content type doobs doget for model method and that's that method that's added by the manager the content type manager and all we need to do to that is pass in a model so I'm just going to pass the rating model in and what that's going to do is return the content type instance that represents the rating model so let's print this out and see what we get if we print out rating content type and execute this script we get back this here and that is actually the string representation of a content type model and as before if we have a content type model we can access the app label and the model so for example if we look at the app label here we're going to get back that the app is core and you can see that on the right hand side here and as we learned earlier in the video if you have the content type instance what you can do is actually look up the actual underlying model and we can do that by referencing the content type and then we can call model class which is a method on that content type and we can store that in a variable let's just call that c and then if we print that to the terminal and run the script on the right hand side we're going to get back the original model that we used which is the rating model so the initial statement here allows you to get the content type model instance for a particular model in the application and this statement here when we call the model class method on the content type that allows you to take the content type and actually get back the original model class that is represented by that content type so that's all for Content types in this video we've covered the basics we've seen the database table for the content type models and that tracks all of the installed apps in your application and then we've seen how to do some querying with the content type model class and this class has specific methods that you can use in order to go from content type to the actual instance and also to do lookups on the underlying instance represented by that content type and similarly we have methods on the content type manager that allows us to get back the content type for a specific model class we can use these in particular with generic foreign keys and we're going to see that in the next video we're going to move on to some more interesting things that you can do with the content types framework we'll see that in the next video but thank you for watching this video if you've enjoyed it please like And subscribe to the channel that would be greatly appreciated it would help the channel a lot and we'll see you in the next video
Info
Channel: BugBytes
Views: 3,890
Rating: undefined out of 5
Keywords:
Id: S85MOayADz8
Channel Id: undefined
Length: 15min 6sec (906 seconds)
Published: Wed Nov 29 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.