Abstract class in Django model (abstract)

First, let’s introduce what attributes the django model has: Let’s look at an example first:

The Meta of the Django model class is an internal class, which is used to define the behavior characteristics of some Django model classes. The following summarizes this:

The abstract property defines whether the current model class is an abstract class. The so-called abstract class does not correspond to a database table. Generally we use it to summarize some public property fields, and then the subclasses that inherit it can inherit these fields. For example, in the following code, Human is an abstract class and Employee is a subclass that inherits from Human, so when running the syncdb command, it will not generate a Human table, but will generate an Employee table, which contains the fields inherited from Human. Inherits Human’s public attributes.

class Human(models.Model):
    name=models.CharField(max_length=100)
    GENDER_CHOICE=((u'M',u'Male'),(u'F',u'Female'),)
    gender=models.CharField(max_length=2,choices=GENDER_CHOICE,null=True)

    class Meta:
        abstract=True

class Employee(Human):
    joint_date=models.DateField()

class Customer(Human):
    first_name=models.CharField(max_length=100)
    birth_day=models.DateField() 

The above code, the output after executing python manage.py syncdb is entered below, it can be seen that the Human table has not been created

$ python manage.py syncdb 
Creating tables ... 
Creating table myapp_employee 
Creating table myapp_customer 
Installing custom SQL ... 
Installing indexes ... 
No fixtures found. 

Leave a Reply