

Django Models - Digging a Little Deeper
source link: https://alexgaynor.net/2008/nov/13/django-models-digging-a-little-deeper/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Django Models - Digging a Little Deeper
Thu, Nov 13, 2008For those of you who read my last post on Django models you probably noticed that I skirted over a few details, specifically for quite a few items I said we, “added them to the new class”. But what exactly does that entail? Here I’m going to look at the add_to_class method that’s present on the ModelBase metaclass we look at earlier, and the contribute_to_class method that’s present on a number of classes throughout Django.
So first, the add_to_class method. This is called for each item we add to the new class, and what it does is if that has a contribute_to_class method than we call that with the new class, and it’s name(the name it should attach itself to the new class as) as arguments. Otherwise we simply set that attribute to that value on the new class. So for example new_class.add_to_class(‘abc’, 3), 3 doesn’t have a contribute_to_class method, so we just do setattr(new_class, ‘abc’, 3).
The contribute_to_class method is more common for things you set on your class, like Fields or Managers. The contribute_to_class method on these objects is responsible for doing whatever is necessary to add it to the new class and do it’s setup. If you remember from my first blog post about User Foreign Keys, we used the contribute_to_class method to add a new manager to our class. Here we’re going to look at what a few of the builtin contribute_to_class methods do.
The first case is a manager. The manager sets it’s model attribute to be the model it’s added to. Then it checks to see whether or not the model already has an _default_manager attribute, if it doesn’t, or if it’s creation counter is lower than that of the current creation counter, it sets itself as the default manager on the new class. The creation counter is essentially a way for Django to keep track of which manager was added to the model first. Lastly, if this is an abstract model, it adds itself to the abstract_managers list in _meta on the model.
The next case is if the object is a field, different fields actually do slightly different things, but first we’ll cover the general field case. It also, first, sets a few of it’s internal attributes, to know what it’s name is on the new model, additionally calculating it’s column name in the db, and it’s verbose_name if one isn’t explicitly provided. Next it calls add_field on _meta of the model to add itself to _meta. Lastly, if the field has choices, it sets the get_FIELD_display method on the class.
Another case is for file fields. They do everything a normal field does, plus some more stuff. They also add a FileDescriptor to the new class, and they also add a signal receiver so that when an instance of the model is deleted the file also gets deleted.
The final case is for related fields. This is also the most complicated case. I won’t describe exactly what this code does, but it’s biggest responsibility is to set up the reverse descriptors on the related model, those are nice things that let you author_obj.books.all().
Hopefully this gives you a good idea of what to do if you wanted to create a new field like object in Django. For another example of using these techniques, take a look at the generic foreign key field in django.contrib.contenttypes, here.
Recommend
-
52
You may have wondered lately what all the buzz is about Context and what it might mean for you and your React sites. Before Context, when the management of state gets c...
-
38
This blog post continues the exploration of the new SSA-based intermediate representation through multiple examples. Make sure to read theIntroduction to SSA if yo...
-
7
Today, we continue our journey into Ruby Templating. With the lexer in place, let’s move on to the next step: The parser. Last time
-
78
Velociraptor - Endpoint visibility and collection tool. Velociraptor is a tool for collecting host based state information using Velocidex Query Language (VQL) queries. To learn more about Velociraptor, read the documentation...
-
3
A Little Django Notes 发表于 2021-02-27...
-
12
Digging deeper into Quarry Posted on: September 2, 2021 September 8, 2021
-
14
This article was published as a part of the Data Science Blogathon. Introduction on Long Short Term Memory Have you se...
-
6
💩🐞 — Dung beetle moms protect their offspring from a warming world by digging deeper Climate change has forced dung beetles to modify their nesting behaviors....
-
10
container-queries Digging Deeper Into Container Style Queries ...
-
10
How to Communicate When Trust Is Low (Without Digging Yourself Into A Deeper Hole) This is based on an internal quip doc I wrote up about careful communication in the context of rebuilding trust. I got a cou...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK