First, add new attributes to the class:
>>> class_changer("models.Staff", "gender", BooleanField()) # Add new field to Staff >>> class_changer("models.Staff", "country", ForeignKey("Country")) # Add new field to Staff
For these two, simply click Staff directly:
models.Staff.gender = BooleanField() models.Staff.country = ForeignKey("Country")
Or make it general:
def add_to_class(cls, name, attr): setattr(cls, name, attr) add_to_class(models.Staff, "gender", BooleanField()) add_to_class(models.Staff, "country", ForeignKey("Country"))
Secondly, creating a new class:
>>> class_changer("models.Country", "name", StringField())
You can create a class in a function and then assign it to a module:
def new_class(mod, name): class New(object): pass setattr(mod, name, New) new_class(test, "Country") add_to_class(test, "Country", StringField())
I'm not sure if you want to combine new_class and add_to_class , but I suppose you could do:
def create_if_needed_and_add_to_class(mod, clsname, attrname, value): if clsname not in dir(mod): new_class(mod, clsname) add_to_class(mod, attrname, value)
and then finally for your class_changer :
def class_changer(mod_clsname_string, attrname, value): modname, clsname = '.'.split(mod_clsname_string) create_if_needed_and_add_to_class(globals()[modname], clsname, attrname, value)
Edit: fixed class_changer to use locals() to search for a module name, since it is a string, not a module.
Edit: oops, this should be globals() .