Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - how to extend 3rd party models without modifying

I want to add a column to a database table but I don't want to modify the 3rd party module in case I need/decide to upgrade the module in the future. Is there a way I can add this field within my code so that with new builds I don't have to add the field manually?

like image 880
user409880 Avatar asked Aug 08 '10 04:08

user409880


2 Answers

Take a look to Django model inheritance and abstract classes http://docs.djangoproject.com/en/dev/topics/db/models/#multi-table-inheritance

like image 121
cues7a Avatar answered Oct 16 '22 16:10

cues7a


You can use ModelName.add_to_class (or .contribute_to_class), but if you have already run syncdb, then there is no way to automatically have it add the columns you need.

For maintainable code, you will probably want to extend by sub-classing the desired model in your own app, and use something like south to handle the database migrations, or just use a OneToOneField, and have a related model (like UserProfile is to auth.User).

like image 42
Matthew Schinckel Avatar answered Oct 16 '22 14:10

Matthew Schinckel