Djangoの抽象ベースクラス
1815 ワード
Djangoには3つの継承方法があります.
- : Abstract base classes, , , :
- :Multi-table inheritance, ;
- : Python , , 。
from django.db import models
class CommonInfo(models.Model):
name = models.CharField(max_length=100)
age = models.PositiveIntegerField()
class Meta:
abstract = True
class Student(CommonInfo):
home_group = models.CharField(max_length=5)
Student name,age,home_group , CommonInfo ,Djiango
。
Meta , Meta 。 Meta:
from django.db import models
class CommonInfo(models.Model):
# ...
class Meta:
abstract = True
ordering = ['name']
class Student(CommonInfo):
# ...
class Meta(CommonInfo.Meta):
db_table = 'student_info'
:
- , , ;
- , , ;
- ;
- abstract=True 。 , , Meta abstract = True;
- , db_table, , 。