how to add new column in django

Solutions on MaxInterview for how to add new column in django by the best coders in the world

showing results for - "how to add new column in django"
Erik
10 Oct 2016
1#add new column to django db
2
3from django.db.models import CharField
4from django.db.models.signals import class_prepared
5
6def add_field(sender, **kwargs):
7    """
8    class_prepared signal handler that checks for the model named
9    MyModel as the sender, and adds a CharField
10    to it.
11    """
12    if sender.__name__ == "MyModel":
13        field = CharField("New field", max_length=100)
14        field.contribute_to_class(sender, "new_field")
15
16class_prepared.connect(add_field)