# One database table, one model, many behaviours: Proxy model with Ronald Maravanyika

Source: https://djangotv.com/videos/djangocon-us/2023/one-database-table-one-model-many-behaviours-proxy-model-with-ronald-maravanyika/
Date: 2023-12-15
Speakers: Ronald Maravanyika

## Summary

Django model inheritance offers three approaches: abstract base classes reuse fields without creating a table, multi-table inheritance adds related tables, and proxy models reuse an existing table while changing its Python-level behaviour. Proxy models are useful when an existing production database needs new classifications, methods, managers, permissions, or admin customisation without a schema change. By combining proxy models with custom model managers, objects from one table can be exposed as filtered groups such as programmers, doctors, or Django users, each with its own methods; overriding save can also make creating those objects more convenient.

## Transcript

### [00:21] Introduction and Zimbabwe

[00:21] Thank you so much. Yeah, so just like the previous speaker, I came a very long way. So my name is Ron. I'm a co-founder of a nonprofit called Zimbopie. We teach young girls how to code. I'm also an individual member of The DSF, I'm also a member of the PSF and I've been organizing Django Girls. I'm a Formula One lover and a cricket fan, which is not something popular on this side of the world.

[01:07] so I thought I came from a long way, so let me just tell you a bit about my country My country is on the southern part of Africa and we own that instrument. It's a musical instrument. It's called Mbira. I'm hoping this will work, but I just want to make you hear how it sounds like. Start from the top. Okay So that's Bira for you, and it's unique to Zimbabwe. One of the things about Zimbabwe, we own

### [01:55] Django Models and the Database API

[01:55] one of the seven natural wonders of the world. So feel free to visit the Vic Falls. There are a bunch of other things that you can do in Zimbabwe. We own the largest min -made lake in the world. So I thought I should share some of these. Anyway, let's get into it. So my talk will be structured in sort of two sections. The first section is sort of beginner and it slowly transition into intermediate. Depending on who you are, you might think that's senior. So I'll be talking about models, model inheritance which will be like the beginner section, and then

[02:46] it will slowly transition into like intermediate to senior where we are talking about why proxy models and the model manager So models are sort of the describing features of your of your of your data. So they give your data structure. And personally, when I came into Django, that is something that drew me closer to Django. Not only models do they come with a An API, it is easy.

[03:33] You don't have to look at the database. You don't have to write queries, things like that. So defining model it's that's how you would define a model, and the cage is that you just have to subclass the models that come with Django. So In Django, it really, really tries to sort of stick to the zen of Python And one of the things that I like about the scene of Python is that

[04:18] simple is better than complex, right? So not only When you write code, you're not writing code just for the machine, you're also writing code for other people. And if your code is not clear enough, that might introduce bugs into your project. The other thing that I like is the fact that It really counts to write readable code. Like I said before, you're also writing the code for the human being. So you have to keep that in mind. So

[05:03] Django is tried to inherit what Python has in terms of inheritance and that's in models So model inheritance, it sort of tries to fulfill that concept of simple is better than complex. You don't have to write the same code multiple times. Okay. So some of the things that that the advantages of inheritance These things are very important when you are writing an application, right?

### [05:06] Readable Model Inheritance

[05:50] And not only will that make your application sustainable, but it will make Other new people want to get into your project, especially in open source, they would get interested in your project because it's easier. So I'm gonna talk about inheritance in models. There are three types. There is abstract and then there is Multi-table inheritance and the other one is the main one that's why we are here. Right? So there is a case where There is an existing database. And in my examples,

### [06:36] Model Inheritance Strategies

[06:36] I made two scoops of junk. So in some of my examples, I might include ice cream. I don't really like it that much, but it's by Danny, ask him. Yeah, so We are going to sort of look at these two inheritance types that we have and then compare it to the proxy model and that How proxy can be very useful for you when you working on an existing project that just needs new functionality. So when you are talking about the abstract

[07:23] base classes you are simply Trying not to repeat yourselves. So if you have fields that you're using in a few other places, it's best to use the abstract base classes. The way you define them is quite simple. You define your model as normal and to declare it you just add in a meta class within abstract equals to true and that will make your model abstract This means that your model doesn't have a database but

[08:12] you can reuse it. So to use it, that's the lower part. That's how you will be able to use it. Then multiple table inheritance. This one is a bit I don't want to say complicated, but there are a few parts to it, right? You have tables But you don't need to add tables to more tables. So you can use the existing tables to link them to your To your new table and the way you declare them, something like that. And there are like multiple types of this type of inheritance, many to many, many to one, one-to-one.

[09:05] So something that you have to be careful when you are using this type of inheritance. This is quite good, but As for a case that you have an existing database, at times that database is already in production, people are using that database, and you want something that will just plug it. Plug it into the existing system. But you don't really want to create a new table, right You just want to use the database that is there and sort of just tune in the Pythonic behavior. So that is where proxy models

### [09:51] Proxy Models

[09:51] shine, right? They are the third layer of inheritance. And the reason why I made this talk is because I don't think they are popular and for me it's a gem that helped me. So hopefully it will help someone here. And basically with proxy models you are not really creating a new database, you're just using what is there? We are using the table that is there, and you can sort of extend from the existing data and we are going to take a look at how that can be done.

### [10:39] Proxy Model Implementation

[10:39] To declare a proxy model I'm going to use an example and I tried to use some of the things that we have been talking about at this conference. So we are going to be using a class called Nerd, right? So the NED class is The existing database that we have, right? And in that NED class, we are going to be adding a few other things we are going to add a programmer we are going to add a doctor and we are going to add a Django

[11:25] nut right so this is the simple declaration that you do for a proxy model and You see just like the abstract one, you call it using the your meta class. You just set it the proxy equals to true. And let's if we look at the console, you see what's happening here is We defined the NED object, right? And we created this Maranika object that has got N, right?

[12:11] And you can see that in the NED object, there is only one item that is there. We did not declare any other item. But if you look at the programmer object, there is also one item that is there, but we did not create it directly. So it means the programmer is using the NED class that already exists. Okay, so this is a very good starting step because you now have access to your existing data that is there. But You might want to have a bit more

[12:58] control over that. And this is something that Django does intentionally because it wants you to have control over what you do So here I'm just adding those few other classes that I was talking about. And the behavior is pretty much the same. We have one class and those objects we find them in multiple places. I've used a couple of my friends' names in here. Hopefully you don't mind.

[13:44] So yeah, yeah, it's still the same structure. But we would want a scenario if I go back, we would want a scenario where We have the existing database of nerds. So let's say we have a database of all Django con attendees and we call them nerds, right? We think that we want to sort of classify them into programmers, doctors, and Django nuts, right? So it would be good to have One callable object for each one of them And this is

### [14:29] Custom Model Managers

[14:29] where we introduce the concept of a model manager. What a model manager is, is It hacks the default model API and you are now trying to create your own query that's different from the generic object manager that comes with Django So to just to remember, if you don't know what a manager is, you are just that's the query section of your of your model. The

[15:14] objects, the dot objects that you see that come with model, that's the default manager. And in this case, we are not going to change that object name. So what you simply do is You declare your manager class, right? And it extends from the default manager model. And the get query set is how can I put it? It's it's similar to like your whatever your class name is dot object. or.

[16:00] So that's what we have here when we say get query set. But on top of that, we want to filter just the programmer side of it. So you add in a filter in front of that, and you have your manager. To use your manager, you change the default objects into use your newly defined Model manager. And I will talk about the method that's down there, the method called fix something. I will talk about it very soon. So

[16:45] now let's look at what happens when we do that, when we add a model manager. You will see that in the first section there we now have a programmer filtered object right but still we didn't define a manager for doctor So if you check what's in Dr. Doctor still has access to everything that is in the database. So what we have done here, we have just changed. The behavior of our model

[17:31] using the same data. Okay So, you can give your Now we have like separate things. We now have programmers, we now have doctors, but we would want to give them certain attributes. That are different from each other. So you will see what I'm talking about in a minute. In the programmer model, we want them to have the ability to be engineers and fix everything.

### [18:17] Custom Model Behavior

[18:17] So you can write your method. Here I just kept it simple to write. I'm an engineer. I fix it all again in the context of some of the talks. At this conference. And you can do the same thing for your manager as well as your Django nut models and you give them different attributes So now you will see that in programma you are able to call the fix something method on it, but you can't call that on the doctor method.

[19:02] We are just changing the Pythonic behavior. We are still using the same table. We are still using the same database. Same applies for Django nuts. have a contribute method within that. So I think it's something that is cool to be able to do that. The last thing that I would like to showcase is the save method, hacking the save method. So you see that previously we were explicitly creating types of objects, right?

[19:48] So we would create the name and then add in the type of that object But we don't want to do it anymore. We want something that's that will be generic. So we hake in the default save method and If you create a new object, you no longer need to specify the NED type that you are using. You just specify the first name and the last name. and your proxy model will work as no more

### [20:28] Overriding Save and Practical Uses

[20:34] Now, let's just take a quick look at some of the use cases that we can we can have with proxy models. You can extensively extend your base user model. You might want to give different users different permissions, and that's something that you can do. With proxy models. Is sometimes used to customize the Django admin. In a cool way I know Danny did a very cool tutorial around how you can customize your Django admin

[21:21] using proxy models. Then like the example that we mentioned now, it it's very, very useful when you are using an existing table But you don't really want to make another table, you just want to change the pythonic behavior of it. So these are the some of the use cases that you can use within your proxy model. So just to summarize, we have looked at Django models and the database access API, which is which I think is why people love Jungo so much.

### [22:07] Summary

[22:07] We have also talked about the types of inheritance, abstract, multi-table, and proxy, abstract We said we are trying to reuse the same fields while a multi-table one, it has got like a different table, a separate table, but you just want to link the two. Proxy model, you have the same table, but you are just changing the Pythonic behavior. Then we also looked at model managers, which

[22:53] I think if you Don't know about managers, that's something that you should look at if you're a Django developer. Then lastly, we talked about how you are you can just add the Pythonic behavior of your model That is pretty much it. You can scan that QR code if you want to link with me on LinkedIn. Thank you so much.
