我使用的是contribute_to_class方法,但我不知道如何使用新迁移在数据库中创建该字段。
发布于 2014-06-20 00:52:26
您可以像这样创建:
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)有关更多信息,请参阅Django Model Field Injection。
https://stackoverflow.com/questions/24311993
复制相似问题