I have been writing my first few Django models and wanted to take a look at the base class that all models extend (example: "class Poll(models.Model") but could not find the Model base class. I checked the source on github and when I browsed to the django.db.models directory I was surprised to not find a "Model.py" file that I could look at. Is this file generated? Or does the class Model live somewhere else? Or is there some python package magic going on that I'm not familiar with?
As has been remarked before, Python is not Java. In particular, there's nothing in Python that says that a class has to live in a file with the same name as the class.
As San4ez points out, the Model
class lives in django.db.models.base
, and is imported into the __init__.py
file in that directory so that it can be referenced directly as models.Model
. This isn't any kind of magic, just normal idiomatic Python.
However, once you look into the code for the class itself, you'll find that it actually does consist of quite a lot of Python magic, specifically around metaclasses. But that's a different question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With