Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is Model.py in the Django source code?

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?

like image 311
Andrew Halloran Avatar asked Apr 14 '12 14:04

Andrew Halloran


1 Answers

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.

like image 94
Daniel Roseman Avatar answered Oct 04 '22 01:10

Daniel Roseman