[Fixed]-Using self in Django Model classes

12👍

Django uses metaclasses to create the actual class based on the class definition your provide. In brief, upon instantiation of your model class, the metaclass will run through your model field definitions and return a corresponding class with the appropriate attributes.

To answer your question directly, using class variables instead of instance variables (object.self) allows the metaclass to inspect the class attributes without having to first instantiate it.

For more information, have a look at the source and the following docs:

Leave a comment