I am having trouble understanding inheritance in django models
If I create a model in django:
from django.db import models
class Person(models.Model):
name = models.CharField(max_length=200)
Can’t I just I write
class Person(Model):
Since I already imported models and I am trying to inherit Model. Is Model a subclass of models? Also can I write the below, since I have imported models already.
name = CharField(max_length=200)
This isn’t a Django question; it’s a Python question.
If you’re not familiar with the Python way of doing things, please read up on it before working in Django.
Investigate Python’s module and import concepts to learn about this. A good place to start is the Modules document in the Python tutorial.
The point is that
from django.db import modelsimports themodelsmodule, so that you have a variable in that scope namedmodelswhich is the models module.It is possible to have something like
from django.db.models import Model, CharField, but for Django models the convention is to import themodelsmodule, rather than its components.