Django models.Model superclass

后端 未结 2 1058
南旧
南旧 2020-12-07 03:04

I would like to create a models.Model class that doesn\'t became part of the database but just an interface to other models (I want to avoid repeating code).

Somethi

相关标签:
2条回答
  • 2020-12-07 03:49

    You can define your classes like this:

    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)
    
    0 讨论(0)
  • 2020-12-07 03:54

    "Abstract base classes"

    Abstract base classes are useful when you want to put some common information into a number of other models. You write your base class and put abstract=True in the Meta class. This model will then not be used to create any database table. Instead, when it is used as a base class for other models, its fields will be added to those of the child class.

    0 讨论(0)
提交回复
热议问题