Django uses the metaclass ModelBase for models (django.db.models.Model) to perform various registration processes.
So I was reading the source
class ModelBase(type):
def __new__(cls, name, bases, attrs):
parents = [b for b in bases if isinstance(b, ModelBase)]
There was a code like this, so when I typed ʻis instance (Question, Model) `[^ 1] with" Hmm "and shell,
False
[^ 1]: Question is a class that inherits Model. That is, Django's model class
If you think about it carefully, you can understand it, but at first I thought "that?", So I will summarize what I understood.
isinstance returns True if the first argument ** object ** is an instance of the second argument ** class ** .. For example
isinstance(123, int)
Returns True.
Question is ** class **, Model is also ** class **. In other words, you will be asked, "Is the Question class an instance of the Model class?" No such thing. Not surprisingly, False is returned. Subclasses and instances were messed up.
Apart from isinstance, there is a function called issubclass. You can use this.
issublass(Question, Model)
True is returned safely.
Let's get back to the Django source. In the Django source, I was checking if the superclasses of "classes to be created" are instances of ModelBase [^ 2]. The first argument is ** class **. The reason for this is that the second argument is a ** metaclass ** that inherits type. ** Since the instance of the metaclass is a class **, this check is True if the class inherits the Model class. That is, the following expression returns True.
isinstance(Question, ModelBase)
[^ 2]: It seems that you can create an abstract model class that defines common attributes in the application, but is it practical to create such a complicated model hierarchy?
Recommended Posts