开发者

Django - how to extend 3rd party models without modifying

开发者 https://www.devze.com 2023-01-10 12:05 出处:网络
I want to add a column to a database table开发者_如何学JAVA 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 fi

I want to add a column to a database table开发者_如何学JAVA 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?


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).


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号