Django: Getting Started Transcripts
Chapter: Django Admin
Lecture: Model object metadata
0:00 The Dunder string method isn't the only control that you have in your model. There is another concept as well.
0:07 You can add meta data that controls an object through the use of an inner class named meta.
0:13 Configuration here changes the behavior of queries on the object as well as how the Django admin uses it.
0:20 You can control the name of the object in the admin, the default sorting order, the name of the table in the database,
0:28 a variety of permissions, what indexes are created in the database, and you can declare constraints, limiting what values are acceptable in the object.
0:38 Let's go add some meta data. I'm back in catalogs models.py file. I'm going to add some meta data to the book class.
0:49 This is the declaration of the inner meta class where the meta data goes. And this is my first example of meta data.
1:00 The ordering attribute takes an iterable of field names, which if you don't use the order by clause in a query, this specifies the default sort order.
1:13 You can even sort by related fields by using the double underscore. The code here sorts books by their related author's last name
1:22 and then by the book's title. Let's do some more. The verbose name and verbose name, plural attributes, indicate what this class is called.
1:37 The key use of this information is the name of the class in the Django admin.
1:43 You can specify just verbose name on its own and the plural version will get an s added.
1:49 Or in the case of something like boxes where the plural spelling is different. You can specify both terms when these are not specified.
1:59 The django admin uses the model name and the model name plus an S for plural.
2:08 Unless you tell it otherwise, the database will organize the data in question based on the order of insertion. Generally the primary key.
2:17 If you want to search through a database by a field quickly, an index can make a big difference in performance.
2:24 To tell your database that you want an index created for this model. You use the indexes, meta data attribute.
2:31 The example here tells the database that I want an index on the title and on the author.
2:37 This goes along with the ordering clause nicely and will give me a performance boost with my chosen default order by sorting.
2:46 Let's go look at our changes in the browser. I've gone back out to the homepage and here its authors and instead of books its Bookington McBookFace.
2:57 Clicking on Bookington and the ordering in the change list is now ordered alphabetically by author, last name and then by title.
3:08 Dickens first, then Homer.