📜  如何在 django 中添加新列 - Python 代码示例

📅  最后修改于: 2022-03-11 14:45:03.891000             🧑  作者: Mango

代码示例1
#add new column to django db

from django.db.models import CharField
from django.db.models.signals import class_prepared

def add_field(sender, **kwargs):
    """
    class_prepared signal handler that checks for the model named
    MyModel as the sender, and adds a CharField
    to it.
    """
    if sender.__name__ == "MyModel":
        field = CharField("New field", max_length=100)
        field.contribute_to_class(sender, "new_field")

class_prepared.connect(add_field)