Sahand
Sahand

Reputation: 8360

Where do Django model fields reside?

I've looked into the source code for some django model fields, in this case DateTimeField. In the tutorial for Django, we are taught to create a DateTimeField like this:

from django.db import models

field = models.DateTimeField()

But looking in the source code, the file where DateTimeField is defined is django/db/models/fields. So, intuitively, if I were to import the field, I would write from django.db.models.fields import DateTimeField.

Do you see the difference? In the tutorial, they import it from django/db/models, while it looks like from the location of the source code that it actually resides in django/db/models/fields. Why doesn't the tutorial way of importing DateTimeField crash?

Upvotes: 2

Views: 60

Answers (1)

rubayeet
rubayeet

Reputation: 9400

The fields are imported in django/db/models/__init__.py

Take a look at the source code.

Possibly relevant: What is init.py for?

Upvotes: 3

Related Questions