Search code examples
pythondjangodjango-modelsloose-coupling

Django loose coupling and extending a pre-existing model


Lets say that I create an application blog [which is irrelevant it's just there so I can do app.blog.posts - so it's really just a folder] and then in blog I create the app posts inside of blog and posts defines the model post. After I do this I would then create an application categories in blog which should extend the model posts (or any model like posts -- adding the category field to post) and then creating it's own table. If these applications were entirely unrelated (exg: django-tags) how would categories be able to extend post without ultimately being aware of it? I am a bit confused about django loose coupling so perhaps somebody can explain it to me please (unless there are already examples but I couldn't find a good example that follows this style of thinking.)


Solution

  • If "extend" means to somehow add functionality, in general, signals are are great idea. Using signals, you can hook into some of a model's methods without even modifying the model files.

    If you simply need your blog posts to have categories, what's the problem using a foreign key in blog posts?

    It's always a good idea to design django apps as generic as possible, means, make it an own app if it's likely going to be reused.