Django Classy All The Things!!!

This video features Emma Delescolle at DjangoCon Europe 2024 in Vigo, Spain.

Django Classy All The Things!!!
0:19:42
Published July 11, 2024
415 views

Talk: Django Classy All The Things!!! by Emma Delescolle

https://pretalx.evolutio.pt/djangocon-europe-2024/talk/GYMVHC/

Summary

Python’s object model makes code highly introspectable: strings, functions, classes, and even built-in types are objects, with tools such as `dir()`, `__doc__`, `type`, MRO, and `inspect` revealing how they work. Emma connects this idea to Classy Class-Based Views (CCBV), which presents Django’s class-based views with inherited attributes, methods, docstrings, and source code in one place, making framework internals easier to understand than scattered base classes and mixins. She argues that this approach is useful beyond Django’s built-in views and demonstrates a tool for generating similar, offline HTML documentation for a project’s own models and other classes. The generated pages show fields, methods, inheritance details, source locations, and configurable exclusions for framework-provided or third-party components, so developers can inspect large codebases even when the Django development server is not running. She invites attendees to help improve and publish the tool.

Key takeaways

  • Python’s everything-is-an-object model enables extensive introspection of values, functions, classes, and types.
  • `dir()`, `__doc__`, `type`, method resolution order, and `inspect` help reveal how Python and Django code works.
  • CCBV gathers inherited Django class-based view attributes, methods, documentation, and source code into a locally navigable reference.
  • A similar tool can generate offline HTML documentation for a project’s models, fields, methods, and inheritance hierarchy.
  • Because the documentation is stored on disk, it remains available when the Django development server or application is broken.
  • Configurable known apps and hidden framework-defined fields keep generated documentation focused on the project’s own code.

Summarised automatically from the transcript.

Transcript

2,574 words · auto-generated Show

Automatically transcribed, so expect mistakes in names and technical terms.

0:05

Hello, uh my name is Emma and uh I've been doing Python and and Django for quite a few years now. Uh I'm Also, a proud member of the the PSF as well as the DSF. And I co-founded a small company called Levit. And with Levit, we do a lot of uh data management for application, things like CRPs, CRMs. Um things like a lot of other speakers uh on this stage uh this week have are doing. We're working on large applications. And this is going to be well in a couple minutes. Just a short disclaimer.

0:51

I don't have Karen 's ability to draw, nor could I find a lot of images that were really related to the topic On the internet, so I asked AI, I asked Crayon to help me find images for this talk. And I've had great success doing that before. I've if you've not seen my talk about the Raspberry Pi and a chipset on a Raspberry Pi. Raspberry Pi, try to imagine what AI would draw for a chipset on a Raspberry Pi. So for starters, I want to talk about mathematics. Mathematics is a language that is Created by humans but is the only language that can describe mathematics.

1:37

And mathematics is the base of our s our understanding of everything. Mathematics is the base of physics. Physics is the base for astrophysics. Everything is based on something that describes itself. And astrophysicists use math, but they also use another language that can be used to describe itself. It's Python. Python can be used to describe itself. This is the concept behind things like PyPy, uh Pyth uh Python interpreter written in Python. And the reason that Python can describe itself is because in Python everything is an object.

2:22

A string. And you you can read this code, right? Um so So uh in Python everything is an object. A string is an object. But literals like integers and booleans, they're also objects. Um of course if you create your own class and you instantiate that class That's an object. The instance of the class is an object. But the class itself is also an object. It's an object of type type. And objects, the the the built-in object in Python is also an object. That that's in the name it would it would it would be bad if

3:07

object was not an object. But object is a ty is an object of type type and type itself is an object of type type. And really when I say everything is an i is an object in Python, functions are objects also in Python. So if you're doing function based use because you don't like to do object oriented. Well I'm sorry to ruin your day, but you're doing class based use actually. Um I lost my mouse. Here. Uh the nice things about objects is that objects can be introspected and they can be inspected.

3:53

You have things like dirt that will let you know all the properties, methods of an object. Uh there is Dunder Doc that will let you read uh the the docs that you vote for a function or any other type of object. There is type that will give you uh the parent class of an object. There is MRO that will let you know when you're going to call a method on an object. MRO is going to tell you, okay, I'm going to call the same method on this parent first and then on that other parent and on that class parent after. So all these things, all these tools are built into Python to let you understand how Python itself works, how your Python code works.

4:45

or the code that somebody else's uh wrote works. So um Python even comes with specific modules that let you uh know more about your code or any code basically in Python. You can try importing inspect if you've never done that. Import inspect and then you can learn a lot of things about anything you want in any code base in Python. But what what does it really have to do with the with the title of the talk? I mean the title of the talk was Classy All the Things.

5:30

Well the t the the ta the title of the touch sorry. The title of the talk comes from this thing called classy class-based views. So, for those of you who don't know what classy class-based, Best views is this is uh what some people call the second uh documentation website of Django. It is a website that's oh that that is the the AI generated image. This doesn't help you. This is a a screenshot, it's better.

6:17

Um so this is a website that lets you learn about class based views in Django. And the problem with class based views is that if you are opening a template view for example when you're trying to understand what is going to be doing when you uh try to use it, there is not much in the in the template view itself, because all the code that's used to render a template view is It's uh spread onto other base classes, onto mix-ins and things like that. And uh Corton has told us, uh locality is something that is quite nice when When you open a file you see everything that it does and it allows you to understand things.

7:05

This is something that we don't have uh out of the box with class based views. When you open a template view it I think it it it gives two methods. actually that you can that you can see in in in in the class based view. Everything else is defined in other classes. So uh here CCB uh lets you uh inspect everything I'm trying I'm going to try to switch to a live view. So this is the actual website of class And if I scroll down to template viewers.

8:01

Now for for this template view I'm able to see everything that is defined in a template view. I've got all the the properties, the attributes, and if I scroll down I've also got all the methods that a class based view actually owns. And even if if I click on one of those I lost my screen. Okay. If I click on one of those uh not only will it give me the doc string for that method, but it will also give me the code for that method. And um as the person who did the the talk about uh using and abusing the admin yesterday said, uh

8:46

digging to the code of the framework that you're using is something that is great. It's how you can learn about your framework, it's how you can have a deep insight on exactly how uh the framework that you're using is doing the things that you want it to do and it help often helps you find bugs more easily and things like that. So personally I find that this this kind of tool is is very useful and it fixes the locality problem. No if I crawl back if I scroll back up I see everything that uh that I want to know about this template view. Um and Apparently I'm not the only one who finds this interesting

9:34

uh because once C C B V was out, um other people started doing the same thing with uh other part of Django. Um we have classy Django Rest framework. I'm I'm really not sure how the AI got this thing. Um but this this this is a representation of of Class C DRF. Um and Class C DRF gives you the same kind of information except with uh serializer, um uh view sets and and that kind of thing. Um And uh there is also uh classy Django Forms.

10:19

This one is a bit clearer how we got there. Um unfortunately classy Django Forms uh hasn't been updated in a while, it's still at Django tree. So it is still useful because the hierarchy of things hasn't changed that much, but uh it's not up to date anymore. And but once you you start using that kind of documentation that kind of tool it it's more than documentation, it's a tool. It is it becomes similar to your own code editor. You know when you're in your editor and you jump to the definition of a of a method. This is this is what it does. And um for

11:04

that reason s some people love it, love to to to to have that kind of tool, to borrow that kind of tool. Some hate it because uh you shouldn't need uh a second documentation website. I'm part of the people who loves it because I use a lot of uh class-based things. Uh maybe soon I will be using class-based emails as well. Who knows? Um but uh I've been using it all the time. Uh and I don't know what happened to that tiger. Um but I've I've been using it all the time. It has made my life uh so much uh easier. It has brought me a much better understanding of how Django's

11:52

internal works. But at the same time it has bought it has brought some frustration. Uh I am frustrated because uh now I can see everything I want to see about Django class based views, but I don't really know anything about Django admin options or I don't really know anything about my own code or I can see an outdated version of what Django forms do, but I cannot see exactly what the latest version So I'm thinking, wouldn't it be great if I could have a Django classy

12:38

of my own codes? Uh because you know, remember I said we worked on large Spandish project. Uh so when there's this project that has a hundred uh models, that has two hundred views, that has the same amount of serializers, and When I go back to that project six months later, do I always remember where I bought everything? Absolutely not. Um so th this is something that that that would really Help me. And could I could I really jangle class in my own form? Uh well

13:23

I I I kind of did a thing and uh I I I tried to type manage the bike. Classify and I I I I got some results. And I'm going to try to show that to you. Can everybody well the text on the screen doesn't really doesn't really matter. Uh but uh this is uh output that you can get without too much effort for your own uh for for your own code. And uh this is

14:09

one of these projects that that I told you about. If I start scrolling down you you can see that There's quite there's quite a few things in there. There's quite several models and and things like that. And so now at this stage I don't really remember what's what's in my in my product model, but I I can go there and I can have something similar uh to uh what C C B V offers. Um and also I'm going to to test it right here in front of you. I'm I'm a profound lover of dark mode for several reasons, but if you really want to have it in light mode

14:54

and be blinded by it, uh this is this is also available. Uh so things Django and uh here I'm talking about models. Uh I had to to also make a a a few things. So CCBV tells you about the attributes and methods, but here we're seeing the code for a model. And models they they have this extra thing that is called fields. So this is something that we can also have uh

15:40

as a separate thing. And here you can you can see the all the fields uh uh where they are actually defined in in what uh file they are designed. Um and uh here we have some checkbox on the top where I can uh hide and show part of the code uh part of the output. So so for example PK is defined by Django. Most of the time I'm not going to want to see PK as the l in in the list of my fields because this is something defined by Django. I know there is a PK. So by default this is hidden. Um if I go see the the methods, um this is very similar to how CCBV works.

16:27

I can also click on it and have the Details um I I'm clicking on my back in my back so I didn't pick something that has inheritance, but if it has inheritance and for example method is defined on several level. It will also give you uh every uh thing that defined that method uh before the class. Um so yeah, this is uh this is something that you can now do. And uh if I scroll back up you can you can see also that I've got a uh a second checkbox. And uh this is a checkbox for DRS schema adapters, which is also something I built.

17:13

But you probably don't care But the R schema adapter because you might not have it in your code. Uh so I'm also going to show you how I'm able to get that. Um there is uh a configuration settings where you can configure exactly what what modules you want to document uh with uh that classifying tool. Um and you can also define some known apps, uh the the thing at the bottom. And known apps is a dictionary of list and so you say that for example uh you want you're using wagtail and so you know that wagtail everything that's in the module wagtail triggered or model cluster this is part of wagtail and you don't want to document

18:00

So this is a known app and for every known app you will get a checkbox. Um this documentation is generated and it lives on your hard drive as HTML5. file it's not being served by uh by the Django server. Let me get uh a drink. It's not generated uh it's not life with the Django server because I found out that the moment that I really, really want to know why that method where that method was defined and what it does exactly is when my code doesn't run. So I didn't want to to depend on the on the Django

18:48

development server to serve that. So it's generated and lives on on the hard drive. So even if your development server is completely crashed and nothing runs, you still have access to that documentation. All you need is to have Python, which you probably do if you're working on a Django project, and uh start uh Python's HTTP server. Um so if you want to uh if if you want to uh see more of this and maybe help me uh to publish it to PyPi you can come uh this weekend at the sprint um maybe suggest uh more improved And things.

19:33

But if not , right now it's time for questions.

Questions this talk answers

How can I inspect and understand Python objects and Django code?

Python provides introspection tools such as `dir`, `__doc__`, `type`, method resolution order, and the `inspect` module. These reveal an object’s members, documentation, type hierarchy, method lookup order, and other implementation details.

Discussed at 3:53

What is Classy Class-Based Views (CCBV) and how does it help with Django class-based views?

CCBV is an interactive reference that shows a Django class-based view’s inherited attributes, properties, methods, docstrings, and source code. It makes the framework’s behavior easier to understand by solving the locality problem of implementations spread across base classes and mixins.

Discussed at 5:17

Can I generate Classy-style documentation for my own Django project?

Yes. Emma demonstrates a tool that generates Classy-style HTML documentation for a project’s models, fields, methods, and inheritance, with options to hide framework-provided members and configure documented modules and known apps.

Discussed at 12:18

Why is the generated Django project documentation stored as HTML files instead of being served by Django?

It is stored on disk so it remains available when the Django development server or the project itself is broken. You only need Python to serve the files with Python’s HTTP server.

Discussed at 18:00

Note: We understand that names change, people change, and bodies change. We respect each individual's journey and privacy. If you have any concerns about a video or need us to remove content, please don't hesitate to contact us. We will handle your request with care and promptly address any issues.

More videos by Emma Delescolle

More videos from DjangoCon Europe