Models (and get_model) in reusable Django apps by James Bennet

In this great talk by James Bennet (at about 20mins 30s) he discusses breaking up your site into lots of small reusable django apps:

He talks about how to separated out a contact form into its own Django App. This sounds great. He mentions you can pass in your own Form to customise the sign up application.

What I don’t understand though, is that often the model is the central point/foundation of an app. And if one is passing in a different form that has different fields, then that form must be linked to a different model.

Where does that model live in this reusable app architecture? If the model lives outside the app, its hard to imagine coding an app without the Django encouragement of “fat models”?

I have tried creating reusable apps in the past, buy always get tripped up by the models. How to customise then slightly for each use case. I then try to use this pattern (which is not great, but the best I came up with):

  1. You write a Mixin that contains the extra fields you want. In this example of the sign up form, the mixin could have an extra field say favourite_color.
  2. The Signup form uses this mixin to add the extra fields to its own model
  3. You then import/can use this Signup Model class.

Later James talks about get_model. Is there a matching put_model? I.e. if we are going to load a sign up form model from somewhere else, how do we specify where that somewhere else is? Maybe I am thinking about it wrong. But get_model is supposed to be a more dynamic way of getting a model, which means there must be a dynamic way of assigning models from a diff app to another app. In this case I am guessing I would specify my own sign up model in my own app, then somehow tell django this is the sign up model to be used by the sign up app?

Keep in mind that the Django Admin is “just” an app.

It dynamically builds forms from arbitrary models based upon introspection of those models.

There’s nothing special being done in the Admin that you couldn’t do yourself.

You can create apps that don’t have models of their own - they work on models defined by other applications. (The Django Admin works by you “registering” your models with the Admin application. Other methods are possible.)

(Disclaimer: I am not familiar with the video you are referencing - these are just notes of general principles for creating forms, templates, etc for arbitrary and not-pre-identified models.)