Where is Model.py in Django source code?

I am writing my first few Django models and wanted to take a look at a base class that all models extend (for example: “Poll class (models.Model)”, but could not find the base class Model. Source on github , and when I looked at the django directory . db.models , I was surprised that I did not find “Model.py" that I could look at. Is this file generated? Or does the class of the class live somewhere else? Or is there some kind of magic for the python package I am not familiar with?

+6
source share
2 answers

As noted earlier, Python is not Java. In particular, nothing is said in Python that the class should 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 this directory, so that it can be referenced directly as models.Model . This is not some kind of magic, just normal idiomatic Python.

However, as soon as you look into the code for the class itself, you will find that in fact it consists of quite a lot of Python magic, especially around metaclasses. But that is another question.

+5
source

At https://github.com/django/django/blob/master/django/db/models/__init__.py

view

 from django.db.models.base import Model 

So, the Model class is described there https://github.com/django/django/blob/master/django/db/models/base.py

Literally, you can import a model this way from django.db.models.base import Model into your project. But the django kernel developers decided to hide some service classes, import some of them into the django.db.models package django.db.models and suggest you use a shorter import.

+2
source

Source: https://habr.com/ru/post/913203/


All Articles